从类型转换为DBNull'输入' String'无效.10

时间:2015-04-02 06:32:19

标签: vb.net

使用下面给出的代码显示一个错误。错误是:Conversion from type 'DBNull' to type 'String' is not valid.帮我找到合适的解决方案。谢谢。

代码:

 cmd1 = con1.CreateCommand
 cmd1.CommandText = "SELECT * from staff_profile where staff_id='" + STFID + "'"
 sdr1 = cmd1.ExecuteReader
 If sdr1.Read = True Then
   'Image1.ImageUrl = sdr1("photo_url")
    Dim N1, N2, N3 As String
    N1 = sdr1("first_name")
    N2 = sdr1("middle_name")
    N3 = sdr1("last_name")
    Label15.Text = N1 + " " + N2 + "" + N3
    Label16.Text = sdr1("designation")
    Label17.Text = sdr1("department")
    Label18.Text = sdr1("date_of_birth")
    Label19.Text = sdr1("age")
    Label20.Text = sdr1("father_name")
    Label21.Text = sdr1("permanant_address")
    Label23.Text = sdr1("mobile")
    Label26.Text = sdr1("dateofjoin")     
End If
sdr1.Close()
con1.Close()

错误屏幕:

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这样:

N2 = sdr1("middle_name").ToString()

或者您可以先检查它是否为空:

If Not IsDBNull(sdr1("middle_name")) Then
    N2 = sdr1("middle_name")
End If