I want to bring records stored in my SQL server DB to my form in VB.NET, I did below coding and it works fine but is there any other better way to handle NULL value from DB column that is going to be displayed in the textbox?
If DBNull.Value.Equals(dt.Rows(0).Item("fine_amt")) Then
txtFine_amt.Text = ""
Else
txtFine_amt.Text = dt.Rows(0).Item("fine_amt")
End If
If we don't handle Null value then it is going to throw an error: Conversion from type 'DBNull' to type 'String' is not valid
If I'm reading your question right, it sounds like you could just do this:
txtFine_amt.Text = dt.Rows(0).Item("fine_amt").ToString()
For null values, ToString()
will always just return an empty string.
Not sure if it's really "better" but you could extract the value to a variable and use a conditional operator:
Dim value As Object = dt.Rows(0).Item("fine_amt")
txtFine_amt.Text = If(DBNull.Value.Equals(value), "", value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.