hello, i need some help here,

i already bind my database coloumn 'NAMA' to my combobox 'cbNAMA', and now i want to show data from my database on textbox by selecting the row using cbNAMA but i got this error "System.InvalidCastExeception: Conversion from type 'DBNULL' to type 'String' is not valid" when i choose the coloumn that contain null value, i try to handle by using this code

        Dim int As Integer
        Dim command As SqlClient.SqlCommand
        Dim adapter As New SqlClient.SqlDataAdapter
        Dim dts As New DataSet
        Dim i As Integer
        Dim sql As String
        lblNDP.Text = int

elseIf cbsem.Text = 2 Then 
            Dim strSQL As String = "Select * From S2"
            Dim DA As New SqlClient.SqlDataAdapter(strSQL, conn)
            Dim DS As New DataSet
            DA.Fill(DS, "S2")
            lblNDP.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("NDP")

            sql = "Select JUMLAH from S2"
            command = New SqlClient.SqlCommand(sql, conn)
            adapter.SelectCommand = command
            adapter.Fill(dts)
            conn.Close()
            Try
                If IsDBNull(dts.Tables(0).Rows(i).Item(0)) Then
                    MsgBox("Tiada Markah Untuk Di Paparkan ")
                Else
                    txtSub1.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("PL2")
                    txtSub2.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("VP1")
                    txtSub3.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("SDP")
                    txtSub4.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("NA")
                    txtSub5.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("MA")
                    txtJumPurata.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("JUMLAH")
                    txtNilaiGred.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("NILAI")
                    txtTahapKecemerlangan.Text = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("TAHAP")
                    DTP1.Value = DS.Tables("S2").Rows(cbNama.SelectedIndex).Item("TARIKH")
                End If
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try

this code is under cbNama, i think if i use this code under cbNama, it will started functioning when i clicked the cbNama, i hope you can understand what i try to explain...please help me....

Depending on what the underlying database is, there are functions which wull replace DBNULL with a valid value. For example, in MS SQL you can use the COALESCE function as follows

SELECT lname, fname, COALESCE(mname,"(none)") FROM myTable

This will return values for all three fields, even when there is no middle name entered. In that case, the value returned for mname is "(none)".

You could also try the ISNULL clause e.g.

SELECT ISNULL(MyCol, '') as MyCol From MyTable

Would return either the value in the MyCol column or an empty string if it was null

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.