簡體   English   中英

VB.net 輸入字符串的格式不正確

[英]VB.net Input string was not in a correct format

這是錯誤的圖片

不斷出現錯誤

輸入字符串的格式不正確

strSQLStatement = "INSERT INTO Cart (CartID, ProductID, ProductName, Quantity, Price) values('" & strCartID & "', '" & Trim(lblProductNo.Text) & "', '" & lblProductName.Text & "', " & CInt(tbQuantity.Text) & ", " & decPrice & ")"

我的猜測是 CInt 但它適用於另一個類似的應用程序。 不知道發生了什么。 這是代碼

產品詳細信息.aspx.vb

Imports System.Data
Imports System.Data.SqlClient
Partial Class HTML_Product_Detail

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Request.QueryString("ProductID") <> "" Then
            Dim strConn As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionStringOnlineStore").ConnectionString
            Dim connProduct As SqlConnection
            Dim cmdProduct As SqlCommand
            Dim drProduct As SqlDataReader
            Dim strSQL As String = "Select * from Product Where ProductID = " & Request.QueryString("ProductID")
            connProduct = New SqlConnection(strConn)
            cmdProduct = New SqlCommand(strSQL, connProduct)
            connProduct.Open()
            drProduct = cmdProduct.ExecuteReader(CommandBehavior.CloseConnection)
            'drProduct.Read()
            If drProduct.Read() Then
                lblProductName.Text = drProduct.Item("ProductName")
                lblProductDescription.Text = drProduct.Item("ProductName")
                lblPrice.Text = drProduct.Item("Price")
                lblProductNo.Text = drProduct.Item("ProductNo")
                imgProduct.ImageUrl = "images/product-detail/" + Trim(drProduct.Item("ProductNo")) + ".jpg"
            End If
        End If
    End Sub
    Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        ' *** get product price
        Dim dr As SqlDataReader
        Dim strSQLStatement As String
        Dim cmdSQL As SqlCommand
        Dim strConnectionString As String = System.Configuration.ConfigurationManager.ConnectionStrings("ConnectionStringOnlineStore").ConnectionString
        strSQLStatement = "SELECT * FROM Product WHERE ProductNo = '" & lblProductNo.Text & "'"
        Dim conn As New SqlConnection(strConnectionString)
        conn.Open()
        cmdSQL = New SqlCommand(strSQLStatement, conn)
        dr = cmdSQL.ExecuteReader()
        Dim decPrice As Decimal
        If dr.Read() Then
            decPrice = dr.Item("Price")
        End If
        conn.Close()
        '*** get CartID
        Dim strCartID As String
        If HttpContext.Current.Request.Cookies("CartID") Is Nothing Then
            strCartID = GetRandomCartIDUsingGUID(10)
            Dim CookieTo As New HttpCookie("CartID", strCartID)
            HttpContext.Current.Response.AppendCookie(CookieTo)
        Else
            Dim CookieBack As HttpCookie
            CookieBack = HttpContext.Current.Request.Cookies("CartID")
            strCartID = CookieBack.Value
        End If
        'Check if this product already exist in the cart
        Dim dr2 As SqlDataReader
        Dim strSQLStatement2 As String
        Dim cmdSQL2 As SqlCommand
        strSQLStatement2 = "SELECT * FROM cart WHERE CartID ='" & strCartID & "' and ProductID = '" & Trim(lblProductNo.Text) & "'"
        'Reponse.Write(strSQlStatement2)
        Dim conn2 As New SqlConnection(strConnectionString)
        cmdSQL2 = New SqlCommand(strSQLStatement2, conn2)
        conn2.Open()
        dr2 = cmdSQL2.ExecuteReader()
        If dr2.Read() Then

            Dim intQuantityNew As Integer = dr2.Item("Quantity") + CInt(tbQuantity.Text)
            strSQLStatement = ""
            cmdSQL = New SqlCommand(strSQLStatement, conn)
        Else
            Dim dr3 As SqlDataReader
            Dim strSQLStatement3 As String
            Dim cmdSQL3 As SqlCommand
            strSQLStatement = "INSERT INTO Cart (CartID, ProductID, ProductName, Quantity, Price) values('" & strCartID & "', '" & Trim(lblProductNo.Text) & "', '" & lblProductName.Text & "', " & CInt(tbQuantity.Text) & ", " & decPrice & ")"
            'Response.Write(strSQLStatement3)
            Dim conn3 As New SqlConnection(strConnectionString)
            conn3.Open()
            cmdSQL3 = New SqlCommand(strSQLStatement3, conn3)
            dr3 = cmdSQL3.ExecuteReader()
        End If
        'Response.Redirect("ViewCart.aspx")
    End Sub





    Public Function GetRandomCartIDUsingGUID(ByVal length As Integer) As String
        'Get the GUID
        Dim guidResult As String = System.Guid.NewGuid().ToString()
        'Remove the hyphens
        guidResult = guidResult.Replace("-", String.Empty)
        'Make sure length is valid
        If length <= 0 OrElse length > guidResult.Length Then
            Throw New ArgumentException("Length must be between 1 and " & guidResult.Length)
        End If
        'Return the first length bytes
        Return guidResult.Substring(0, length)
    End Function

End Class

問題幾乎肯定在這里:

CInt(tbQuantity.Text)

異常信息甚至說:

從字符串 "" 到類型 'Integer' 的轉換無效。

如果文本不代表有效值並且空字符串顯然不代表數字,則不能將String轉換為Integer 首先驗證數據,否則使用Integer.Tryparse一次性驗證和轉換。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM