首页 > 解决方案 > 更新记录代码错误

问题描述

我收到一个错误:

System.Data.dll 中出现“System.InvalidCastException”类型的未处理异常

附加信息:无法将参数值从 ComboBox 转换为字符串。

我错过了什么?

Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click

    If txtBursaryR_ID.Text = "" Then
        MessageBox.Show("Empty Id")
    Else
        Dim DBcon As SqlConnection

        DBcon = New SqlConnection("Server=.\FGG;Database=CAOOSC_DB;User=sa;Pwd=c0nstella;")

        Dim command As New SqlCommand("UPDATE tbl_Beneficiaries Set BursaryR_ID=@BursaryR_ID,Donor_ID=@Donor_ID,DonorName=@DonorName,Province=@Province,District=@District,First_Name=@First_Name,Last_Name=@Last_Name,Sex=@Sex,Name_of_School=@Name_of_School,School_Address=@School_Address,Grade_Enrolled=@Grade_Enrolled,Prev_School=@Prev_School,Prev_District=@Prev_District,Intake_Year=@Intake_Year,Child_Vulnerability=@Child_Vulnerability,Children_Disabled=@Children_Disabled,Bursary_Provided=@Bursary_Provided,Supplies=@Supplies,GFirst_Name=@GFirst_Name,GLast_Name=@GLast_Name,Contact_Number=@Contact_Number, ", DBcon)

        command.Parameters.Add("@Donor_ID", SqlDbType.NChar).Value = txtDonor_ID
        command.Parameters.Add("@DonorName", SqlDbType.VarChar).Value = cbDonorName.SelectedItem
        command.Parameters.Add("@Province", SqlDbType.VarChar).Value = cbProvince.SelectedItem
        command.Parameters.Add("@District", SqlDbType.VarChar).Value = cbDistrict.SelectedItem
        command.Parameters.Add("@First_Name", SqlDbType.VarChar).Value = txtFirst_Name
        command.Parameters.Add("@Last_Name", SqlDbType.VarChar).Value = txtLast_Name
        command.Parameters.Add("@Sex", SqlDbType.NChar).Value = cbSex.SelectedItem
        command.Parameters.Add("@Name_of_School", SqlDbType.VarChar).Value = txtName_of_School
        command.Parameters.Add("@School_Address", SqlDbType.VarChar).Value = txtAddress
        command.Parameters.Add("@Grade_Enrolled", SqlDbType.NChar).Value = txtGrade_Enrolled
        command.Parameters.Add("@Prev_School", SqlDbType.VarChar).Value = txtPre_School
        command.Parameters.Add("@Prev_District", SqlDbType.VarChar).Value = txtPrev_District.SelectedItem
        command.Parameters.Add("@Intake_Year", SqlDbType.NChar).Value = cbIntake_Year.SelectedItem
        command.Parameters.Add("@Child_Vulnerability", SqlDbType.VarChar).Value = cbChild_Vulnerability.SelectedItem
        command.Parameters.Add("@Children_Disabled", SqlDbType.VarChar).Value = cbChildren_Disabled.SelectedItem
        command.Parameters.Add("@Bursary_Provided", SqlDbType.NChar).Value = cbBursary_Provided.SelectedItem
        command.Parameters.Add("@Supplies", SqlDbType.VarChar).Value = txtSupplies
        command.Parameters.Add("@GFirst_Name", SqlDbType.VarChar).Value = txtGFirst_Name
        command.Parameters.Add("@GLast_Name", SqlDbType.VarChar).Value = txtGLast_Name
        command.Parameters.Add("@Contact_Number", SqlDbType.NChar).Value = txtGContact

        DBcon.Open()

        If command.ExecuteNonQuery() = 1 Then
            MessageBox.Show("Data Updated")
        Else
            MessageBox.Show("Data Not Updated")
        End If

        DBcon.Close()
    End If
End Sub

标签: sql-server

解决方案


猜测一下,我会说您的代码可能正在尝试将参数的值设置为对象,而您想要将它们设置为这些对象中的文本值。

例如,在第一个中,这是:

command.Parameters.Add("@Donor_ID", SqlDbType.NChar).Value = txtDonor_ID

可能应该更像这样:

command.Parameters.Add("@Donor_ID", SqlDbType.NChar).Value = txtDonor_ID.Text

等等。


推荐阅读