check null rows

xswzaq

Well-known member
Joined
Jul 9, 2007
Messages
61
Programming Experience
Beginner
Me.ATDtxtbox.Text = Me.DataGrid.CurrentRow.Cells("CAUSE").Value



I got this code to get the value from column Cause to fill my text box, it is find with rows that have data, but if empty row it give me error in "convertion from type DBNull to type String is not valid". Is there a way to tell that if row is empty then leave blank and if there is data, get the data to txtbox? Thank You.
 
You shouldn't be using that code anyway. The Text property of a TextBox is type String and the Value property of a DataGridCell is type Object. You should not be assigning an Object reference to a String variable. If the value being assigned is guaranteed to be a String then you should be casting it as such. If it's not then you should be converting it. The good news is that converting DBNull.Value to a String creates an empty string, which is exactly what you want. The following code kills two birds with one stone:
VB.NET:
Me.ATDtxtbox.Text = Me.DataGrid.CurrentRow.Cells("CAUSE").Value.ToString()
 
Back
Top