2016-06-21 3 views
0

に値を追加しようとしたときに文字列からのInt32にパラメータ値を変換できませんでした。ここでは、コードです:は、SQLデータベース

Private m_cn As New SqlConnection 
Private m_DA As SqlDataAdapter 
Private m_CB As SqlCommandBuilder 
Private m_DataTable As New DataTable 
Private m_intRowPosition As Integer = 0 


Private Sub InsertDatabaseItem_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    m_cn.ConnectionString = "Data Source=TREVOR-PC\SQLSERVEREXPRESS;Initial Catalog=Milk Convience Products;Integrated Security=True" 

    m_cn.Open() 
    m_DA = New SqlDataAdapter("Select * From ProductIndex", m_cn) 
    m_CB = New SqlCommandBuilder(m_DA) 

    txtBarcode.Focus() 

End Sub 

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click 
    Dim cmd As New SqlCommand(("INSERT INTO ProductIndex VALUES(" & 
           "@ID," & 
           "@Name," & 
           "@Price," & 
           "@Desc)" & 
           "@Barcode)"), m_cn) 

    cmd.Parameters.Add("@ID", SqlDbType.Int) 
    cmd.Parameters("@ID").Value = txtID.Text 
    cmd.Parameters.Add("@Name", SqlDbType.VarChar) 
    cmd.Parameters("@Name").Value = txtName.Text 
    cmd.Parameters.Add("@Price", SqlDbType.Money) 
    cmd.Parameters("@Price").Value = txtPrice.Text 
    cmd.Parameters.Add("@Desc", SqlDbType.VarChar) 
    cmd.Parameters("@Desc").Value = txtDesc.Text 
    cmd.Parameters.Add("@Barcode", SqlDbType.BigInt) 
    cmd.Parameters("@Barcode").Value = txtBarcode.Text 

    cmd.ExecuteNonQuery() 

    MsgBox("Success!", MsgBoxStyle.Information, "SUCCESS") 

    Me.Hide() 

    txtID.Clear() 
    txtName.Clear() 
    txtPrice.Clear() 
    txtDesc.Clear() 
    txtBarcode.Clear() 

    m_cn.Close() 
    m_cn.Dispose() 
End Sub 


Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click 
    Me.Hide() 
End Sub 

私は、新しいデータベースの項目を入力するbtnOKをクリックした後、例外が発生しますそれは言う:パラメータ値を文字列からInt32に変換できませんでした。

私はあなたがtxtBarcodeをキャストしようとしているコード

答えて

1
cmd.Parameters.Add("@Barcode", SqlDbType.BigInt) 
cmd.Parameters("@Barcode").Value = txtBarcode.Text 

の「値= txtBarcode.Textラインをいくつかのデバッグを行なったし、エラーが「cmd.Parameters(」バーコード@)」の後に発生することがわかりました.TextをBig整数として扱います。これは、明示的に値を変換することによって行う必要があります。テストケースが空の文字列または変換できないその他の文字列値である可能性があります。

あなたのコードを再読み込みしても、IDやPriceパラメータでも問題が発生する可能性があります。