从“DBNull"类型到“String"类型的转换是无效的 vb.net [英] Conversion from type 'DBNull' to type 'String' is not valid vb.net

查看:43
本文介绍了从“DBNull"类型到“String"类型的转换是无效的 vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面给出的代码显示一个错误.错误是:从类型 'DBNull' 到类型 'String' 的转换无效." 帮我找到合适的解决方案.谢谢.

While using the given below code showing one error. The error is: "Conversion from type 'DBNull' to type 'String' is not valid." Help me to find a proper solution. Thank you.

代码:

cmd2.CommandText = "SELECT [first_name]+' ' +[middle_name]+' ' + [last_name] AS NAME, [staff_id] FROM [staff_profile]"
sdr2 = cmd2.ExecuteReader
While sdr2.Read
drop1l.Items.Add(New ListItem(sdr2("name"), sdr2("staff_id"))) // error popup here
End While
sdr2.Close()

推荐答案

你应该这样尝试:

If Not IsDBNull(dt.Rows(0)("name")) Then
    sdr2.Value = dt.Rows(0)("name")
End If
If Not IsDBNull(dt.Rows(1)("staff_id")) Then
    sdr2.Value = dt.Rows(1)("staff_id")
End If

或者像这样的肮脏修复:

or a dirty fix like this:

drop1l.Items.Add(New ListItem(sdr2("name").ToString(), sdr2("staff_id").ToString()))

这篇关于从“DBNull"类型到“String"类型的转换是无效的 vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆