我在这里有一个问题..我无法将组合框内容保存到我的表中...所有其他数据都已成功保存,但组合框保存为1或0 ..看起来有什么不对?
我的表是通过navicat设计的..这与我选择的数据类型有什么关系吗?知道我选择了数据类型为Text
这是我的代码,它没有显示运行错误
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim conStr As String = ("Data Source=localhost;user id=root;password=123456;database=sam;")
Try
Dim con As New MySqlConnection(conStr)
Dim cmd As MySqlCommand
For i = 0 To ComboBox4.Items.Count
con.Open()
Dim sqls As String = "INSERT INTO initial_nom(f_name,s_name,th_name,fo_name,app_no,adm_type) VALUES('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "'," & TextBox5.Text & ",'" & ComboBox4.SelectedIndex.ToString & "')"
cmd = New MySqlCommand(sqls, con)
cmd.ExecuteNonQuery()
Next
Catch ex As Exception
MsgBox("Error in saving to Database. Error is :" & ex.Message)
End Try
End Sub
答案 0 :(得分:0)
SelectedIndex
是整数,指向所选项目的索引。假设组合框的数据源包含字符串集合,请尝试使用SelectedItem
,将其类型转换为字符串:
DirectCast(ComboBox4.SelectedItem, String)