VB.NET中ComboBox绑定数据库并选中后TextBox显示值报错排查
问题
从数据库加载选项到ComboBox后,选择选项时弹出错误:data is null. This method or property cannot be called on NULL values.
需求是点击ComboBox选项时,将对应数据库记录的字段值显示在TextBox中。相关代码如下:
Private Sub CollectionEntry_Load(sender As Object, e As EventArgs) Handles MyBase.Load MysqlConn = New MySqlConnection MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star" Dim READER As MySqlDataReader Try MysqlConn.Open() Dim Query As String Query = "select * from golden_star.loan" Command = New MySqlCommand(Query, MysqlConn) READER = Command.ExecuteReader While READER.Read() Dim sname = READER.GetString("account_name") ComboBox1.Items.Add(sname) End While MysqlConn.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged MysqlConn = New MySqlConnection MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star" Dim READER As MySqlDataReader Dim table As New DataTable Try MysqlConn.Open() Dim Query As String Query = "select * from golden_star.loan where account_name = '" & ComboBox1.Text & "' " Command = New MySqlCommand(Query, MysqlConn) READER = Command.ExecuteReader While READER.Read txtID.Text = READER.GetInt32("id") DatePick.Text = READER.GetDateTime("date") txtName.Text = READER.GetString("account_name") txtCollections.Text = READER.GetInt32("collections") txtContact.Text = READER.GetInt32("contact") End While MysqlConn.Close() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub
解决方法
核心问题
你直接调用GetInt32、GetDateTime这类强类型读取方法时,若数据库中对应字段存储的是NULL值,就会触发该错误——这类方法不支持处理NULL。此外代码还存在SQL注入风险、连接管理不规范的问题。
具体修复步骤
处理字段NULL值
在读取字段前先判断是否为NULL,用IsDBNull方法做安全校验,示例:While READER.Read ' 处理id字段 txtID.Text = If(READER.IsDBNull(READER.GetOrdinal("id")), "", READER.GetInt32("id").ToString()) ' 处理date字段 DatePick.Text = If(READER.IsDBNull(READER.GetOrdinal("date")), "", READER.GetDateTime("date").ToString("yyyy-MM-dd")) ' 处理collections字段 txtCollections.Text = If(READER.IsDBNull(READER.GetOrdinal("collections")), "", READER.GetInt32("collections").ToString()) ' 处理contact字段 txtContact.Text = If(READER.IsDBNull(READER.GetOrdinal("contact")), "", READER.GetInt32("contact").ToString()) ' account_name已在ComboBox中筛选,可直接赋值 txtName.Text = READER.GetString("account_name") End While也可以用更简洁的方式直接读取字段并判断DBNull:
txtID.Text = If(READER("id") Is DBNull.Value, "", READER("id").ToString())改用参数化查询,避免SQL注入
原代码通过字符串拼接生成SQL语句,存在严重的SQL注入风险,替换为参数化查询:Dim Query As String = "select * from golden_star.loan where account_name = @AccountName" Command = New MySqlCommand(Query, MysqlConn) Command.Parameters.AddWithValue("@AccountName", ComboBox1.Text)优化数据库连接管理
使用Using语句自动管理连接的创建与释放,避免手动关闭遗漏导致的连接泄漏:' 在ComboBox选中事件中替换连接创建逻辑 Using MysqlConn As New MySqlConnection("server=localhost;userid=root;password=root;database=golden_star") MysqlConn.Open() ' 执行查询、读取数据的逻辑写在这里 End Using ' 执行到此处会自动关闭并释放连接修复ComboBox初始化时的潜在问题
如果数据库中account_name字段存在NULL值,初始化ComboBox时的READER.GetString("account_name")也会触发相同错误,需要添加NULL判断:While READER.Read() If Not READER.IsDBNull(READER.GetOrdinal("account_name")) Then Dim sname = READER.GetString("account_name") ComboBox1.Items.Add(sname) End If End While
内容的提问来源于stack exchange,提问作者Salwa Hamim
相关产品推荐
相关产品推荐

