使用下面给出的代码时显示一个错误。错误是:“Conversion from type 'DBNull' to type 'String' is not valid.
“帮助我找到合适的解决方案。谢谢。
Code:
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
或像这样的肮脏修复:
drop1l.Items.Add(New ListItem(sdr2("name").ToString(), sdr2("staff_id").ToString()))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)