将计算的文本框值传递给访问数据库

时间:2016-10-19 14:58:03

标签: vb.net visual-studio ms-access

我尝试使用查询将一些文本框值发送到访问数据库。 4个文本框已计算出数值。传递值时,它显示错误从字符串转换为类型double无效。文本框1,2,3,4具有数值。这是我的代码:

Private Sub ok_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ok_button.Click
    provider1 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
    dataFile1 = "D:\electricity_billing.accdb"
    connString1 = provider1 & dataFile1
    myconnection1.ConnectionString = connString1
    myconnection1.Open()
    Dim str1 As String
    str1 = "Insert into 345([ID],[cust_name],[cust_add],[t_p_no],[nic],[email],[total_units],[amount],[adjustments],[due]) values (?,?,?,?,?,?,?,?,?,?)"
    Dim cmd1 As OleDbCommand = New OleDbCommand(str1, myconnection1)
    cmd1.Parameters.Add(New OleDbParameter("ID", CType(TextBox5.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("cust_name", CType(name_textbox.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("cust_add", CType(add_textbox.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("t_p_no", CType(tp_textbox.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("nic", CType(nic.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("email", CType(mail_textbox.Text, String)))
    cmd1.Parameters.Add(New OleDbParameter("total_units", CDbl(TextBox1.Text)))
    cmd1.Parameters.Add(New OleDbParameter("amount", CDbl(TextBox2.Text)))
    cmd1.Parameters.Add(New OleDbParameter("adjustments", CDbl(TextBox4.Text)))
    cmd1.Parameters.Add(New OleDbParameter("due", CDbl(TextBox3.Text)))
    Try
        cmd1.ExecuteNonQuery()
        cmd1.Dispose()
        myconnection1.Close()
        no_textbox.Clear()
        name_textbox.Clear()
        add_textbox.Clear()
        tp_textbox.Clear()
        nic.Clear()
        mail_textbox.Clear()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

0 个答案:

没有答案