locked
If data is NULl/ not there L: Conversion from type 'DBNull' to type 'String' is not valid. RRS feed

  • Question

  • User810354248 posted

    IN my asp.net+vb+sql web in a query i get this error

    Conversion from type 'DBNull' to type 'String' is not valid.

    if the data in that table is null then the above error comes

    presl.Text = dt9.Tables("LEAVE").Rows(0).Item(0)

    Saturday, November 23, 2019 2:44 PM

Answers

  • User288213138 posted

    Hi Baiju EP,

    Conversion from type 'DBNull' to type 'String' is not valid.

     If you want to assign the value to presl.Text, Before that you need to check whether the table has the null value with it or not.

    If Not IsDBNull(dt9.Tables("LEAVE").Rows(0).Item(0)) Then
        presl.Text = dt9.Tables("LEAVE").Rows(0).Item(0)
    End If

    But if you want to assign the null value to presl.Text, you can try to convernt cell value to string.

    presl.Text = dt9.Tables("LEAVE").Rows(0).Item(0).ToString()

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 3:00 AM

All replies

  • User475983607 posted

    if the data in that table is null then the above error comes

    Makes sense.  Simply, check if the value is DbNull before trying to assign the value to presl.Text. 

    https://stackoverflow.com/questions/222834/handling-dbnull-data-in-vb-net

    Saturday, November 23, 2019 3:19 PM
  • User288213138 posted

    Hi Baiju EP,

    Conversion from type 'DBNull' to type 'String' is not valid.

     If you want to assign the value to presl.Text, Before that you need to check whether the table has the null value with it or not.

    If Not IsDBNull(dt9.Tables("LEAVE").Rows(0).Item(0)) Then
        presl.Text = dt9.Tables("LEAVE").Rows(0).Item(0)
    End If

    But if you want to assign the null value to presl.Text, you can try to convernt cell value to string.

    presl.Text = dt9.Tables("LEAVE").Rows(0).Item(0).ToString()

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2019 3:00 AM
  • User810354248 posted

    Thnaks

    Monday, November 25, 2019 6:13 AM