您的问题可能发生在这里:
s = Convert.ToString(ComboBox1.Items(i))
这不会返回值。它返回给定索引处对象的字符串表示形式,在您的情况下显然是 type System.Data.DataRowView
。
您必须转换ComboBox1.Items(i)
为适当的类型并访问其Value
. 或者,由于它是 a DataRowView
,您可以通过适当的列名访问这些值:
Dim row = CType(ComboBox1.Items(i), System.Data.DataRowView)
s = row.Item("column_name")
尽管如此,首先你绝对应该关闭并处理连接,无论事务是失败还是成功。这可以在finally
块中(选项 1)或using
语句(选项 2)中完成。
选项1
// ...
con1 = New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
con1.Dispose()
End Try
选项 2
// ...
Using con1 as New MySqlConnection(str)
con1.Open()
Try
// ...
Catch ex As Exception
Lblmsg.Text = " Error in data insertion process....." + ex.Message
Finally
con1.Close()
End Try
End using