从DBNull到String的转换类型无效

时间:2014-03-20 17:52:17

标签: vb.net vb.net-2010

大家好,我在从数据库在vb.net上显示数据时遇到问题。这是我的问题,我想通过路径在vb.net上显示图像。但是,如果记录中没有图像,它将会落到我将通过路径将其修复为图像的情况下 (抱歉我的英语不好。)

    Public Sub DisplayData()
    txtID.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(0)
    txtlname.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(1)
    txtfname.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(2)
    txtmid.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(3)
    txtstreet.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(4)
    txtBarang.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(5)
    txtcity.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(6)
    txtzip.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(7)
    cmbGender.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(8)
    txtcontact.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(9)
    txtemail.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(10)
    cmbmonth.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(11)
    cmbday.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(12)
    cmbyear.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(13)
    txtage.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(14)
    cmbmarital.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(15)
    txtemerlname.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(16)
    txtemerfname.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(17)
    txtemercontact.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(18)
    cmbcourse.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(19)
    cmbmeet1.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(20)
    cmbmeet2.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(21)
    cmbmeet3.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(22)
    cmbfrom.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(23)
    cmbto.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(24)
    txtPath.Text = DSDrvSchool.Tables("Command Out").Rows(Index).Item(25)
    'pbImage.Image = Image.FromFile(DSDrvSchool.Tables("Command Out").Rows(Index).Item(25))
    If txtage.Text Is Nothing Then
        pbImage.Image = Image.FromFile("D:\JCH Folder\Programming\My1stSystem\DrivingSchoolManagementSystem\Database\Photos")
    Else
        pbImage.Image = Image.FromFile(DSDrvSchool.Tables("Command Out").Rows(Index).Item(25))
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

在访问值...

之前,您应该检查DbNull
' ...

If DSDrvSchool.Tables("Command Out").Rows(Index).Item(25) Is DbNull.Value Then
    pbImage.Image = Image.FromFile("D:\JCH Folder\Programming\My1stSystem\DrivingSchoolManagementSystem\Database\Photos")
Else
    pbImage.Image = Image.FromFile(DSDrvSchool.Tables("Command Out").Rows(Index).Item(25))
End If

End Sub