Home > front end >  how to solve conversion from type 'dbnull' to type 'string' is not valid VB .NET
how to solve conversion from type 'dbnull' to type 'string' is not valid VB .NET

Time:07-15

Hi i'm having an error from my code conversion from type 'dbnull' to type 'string' is not valid

please help.

code:

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value
    txt_DATE.Text = DataGridView1.CurrentRow.Cells(1).Value
    txt_FNAME.Text = DataGridView1.CurrentRow.Cells(2).Value
    txt_LNAME.Text = DataGridView1.CurrentRow.Cells(3).Value
    txt_AGE.Text = DataGridView1.CurrentRow.Cells(4).Value
    txt_SEX.Text = DataGridView1.CurrentRow.Cells(5).Value
    txt_REGNO.Text = DataGridView1.CurrentRow.Cells(6).Value
    txt_ROOMNO.Text = DataGridView1.CurrentRow.Cells(7).Value
    txt_HEIGHT.Text = DataGridView1.CurrentRow.Cells(8).Value
    txt_WEIGHT.Text = DataGridView1.CurrentRow.Cells(9).Value
    txt_CELLNO.Text = DataGridView1.CurrentRow.Cells(10).Value
    txt_ADDRESS.Text = DataGridView1.CurrentRow.Cells(11).Value
    txt_TELLNO.Text = DataGridView1.CurrentRow.Cells(12).Value
    txt_FAXNO.Text = DataGridView1.CurrentRow.Cells(13).Value
    txt_EMAIL.Text = DataGridView1.CurrentRow.Cells(14).Value
    txt_REFERMD.Text = DataGridView1.CurrentRow.Cells(15).Value
    txt_MD.Text = DataGridView1.CurrentRow.Cells(16).Value
    txt_ANES.Text = DataGridView1.CurrentRow.Cells(17).Value
    txt_PROC.Text = DataGridView1.CurrentRow.Cells(18).Value
    txt_INDICATION.Text = DataGridView1.CurrentRow.Cells(19).Value
    txt_XRAY.Text = DataGridView1.CurrentRow.Cells(20).Value
    txt_TARGET.Text = DataGridView1.CurrentRow.Cells(21).Value
    txt_PREP1.Text = DataGridView1.CurrentRow.Cells(22).Value
    txt_PREP2.Text = DataGridView1.CurrentRow.Cells(23).Value
    txt_PREP3.Text = DataGridView1.CurrentRow.Cells(24).Value
    txt_QUALITY.Text = DataGridView1.CurrentRow.Cells(25).Value
    txt_SEGMENT.Text = DataGridView1.CurrentRow.Cells(26).Value
    txt_BBP.Text = DataGridView1.CurrentRow.Cells(27).Value
    txt_PREMED.Text = DataGridView1.CurrentRow.Cells(28).Value
    txt_SCOPE.Text = DataGridView1.CurrentRow.Cells(29).Value
    txt_PROCTIME.Text = DataGridView1.CurrentRow.Cells(30).Value
    txt_FINDINGS.Text = DataGridView1.CurrentRow.Cells(31).Value
    txt_BIOPSY.Text = DataGridView1.CurrentRow.Cells(32).Value
    txt_LOC.Text = DataGridView1.CurrentRow.Cells(33).Value
    txt_CYTOLOGY.Text = DataGridView1.CurrentRow.Cells(34).Value
    txt_SIZE.Text = DataGridView1.CurrentRow.Cells(35).Value
    txt_ENDODESC.Text = DataGridView1.CurrentRow.Cells(36).Value
    txt_THERAPEUTIC.Text = DataGridView1.CurrentRow.Cells(37).Value
    txt_COMPLICATIONS.Text = DataGridView1.CurrentRow.Cells(38).Value
    txt_DIAGNOSIS.Text = DataGridView1.CurrentRow.Cells(39).Value
    txt_OUTCOME.Text = DataGridView1.CurrentRow.Cells(40).Value
    txt_LATECOMP.Text = DataGridView1.CurrentRow.Cells(41).Value
    txt_HISTOPATH.Text = DataGridView1.CurrentRow.Cells(42).Value
    txt_ENDOSPCOPIST.Text = DataGridView1.CurrentRow.Cells(43).Value

End Sub

CodePudding user response:

Add ToString in that code.

 txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()

or put if condition to check value is null or not like below

  IF not DataGridView1.CurrentRow.Cells(0).Value is dbnull Then
    txt_ID.Text = DataGridView1.CurrentRow.Cells(0).Value
  EndIF

But this is very basic level.

Regards Aravind

CodePudding user response:

Try the CheckDBNull function. :)

 ex) txt_PREP1.Text = CheckDBNull(DataGridView1.CurrentRow.Cells(22).Value)

Public Function CheckDBNull(ByVal oData As Object) As String
      Try
         Dim sResult As String
         If TypeOf oData Is DBNull Then
            sResult = ""
         Else
            sResult = Trim(CType(oData, String))
         End If
         Return sResult

      Catch ex As Exception
         Return ""
      End Try
   End Function
  • Related