sql-server - 更新记录代码错误
问题描述
我收到一个错误:
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
解决方案
猜测一下,我会说您的代码可能正在尝试将参数的值设置为对象,而您想要将它们设置为这些对象中的文本值。
例如,在第一个中,这是:
command.Parameters.Add("@Donor_ID", SqlDbType.NChar).Value = txtDonor_ID
可能应该更像这样:
command.Parameters.Add("@Donor_ID", SqlDbType.NChar).Value = txtDonor_ID.Text
等等。
推荐阅读
- nginx - Nginx:如何为特定文件夹下的所有文件设置标题
- java - 如何将依赖锁定用于仅约束的 Gradle `platform`?
- c++ - GCC 向量扩展和 ARM NEON 的内存对齐问题
- python-3.x - pandas :从前一行中减去每一行,并将结果按最大值分组
- javascript - 我正在尝试将 javascript 对象数组中的图像附加到我的 html 页面上
- ms-access - 如何在 Access VBA 中排除列以维护数据
- python - 有没有办法将变量设置为仅另一个变量的前 2 个字符并将其设置为整数
- excel - 如何在vba中通过cpuid锁定excel
- amazon-web-services - API 网关 - 如何在不同的 terraform 部署中拆分嵌套的 REST 资源
- java - Docker 映像无法读取 Spring Boot 应用程序上的 application.yml 文件