Answered by:
How to handle Db nulls

Question
-
User56849873 posted
I am retrieving a database object and trying to set the values of the object's fields to various textboxes in my vb.net page.
However, since some of the database fields have null values, I am getting the following error.
System.InvalidOperationException: Nullable object must have a value.
How do I solve this problem?
Monday, October 11, 2010 4:26 PM
Answers
-
User-442214108 posted
You should handle nulls in much like you should handle exceptions
When populating your textboxes, check System.DBNull.Value first. Textboxes must be populated with a string. If it's null (System.DBNull.Value) either don't populate the Textbox, or populate with empty string (your choice)
you might check out this link:
http://www.aspnet101.com/2008/02/handling-database-nulls-with-a-datareader/- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 11, 2010 4:46 PM -
User3690988 posted
You can also check for NULL in your SQL SELECT statement and set a value there:
SELECT IsNull(OfficeName,'') AS OfficeName, ...
If you have access to the SQL.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 12, 2010 12:25 PM -
User-952121411 posted
Good question, and yes you must check if a value is null 1st or that exception will occur. I will give you the syntax to check for database null values using IsDbNull for pulling feilds directly out of a DataSet and onto a TextBox control. However, the source data object (DataSet) is not the focal point here, you can apply this code to other situations too.
For Each dr As DataRow In ds.Tables(0).Rows If Not IsDBNull(dr("FirstName")) Then Me.txtFirstName.Text = dr("FirstName").ToString() If Not IsDBNull(dr("LastName")) Then Me.txtLastName.Text = dr("LastName").ToString() If Not IsDBNull(dr("Phone")) Then Me.txtPhoneNum.Text = Integer.Parse(dr("FirstName")) Next
Hope this helps!- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 14, 2010 9:28 AM
All replies
-
User-442214108 posted
You should handle nulls in much like you should handle exceptions
When populating your textboxes, check System.DBNull.Value first. Textboxes must be populated with a string. If it's null (System.DBNull.Value) either don't populate the Textbox, or populate with empty string (your choice)
you might check out this link:
http://www.aspnet101.com/2008/02/handling-database-nulls-with-a-datareader/- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 11, 2010 4:46 PM -
User3690988 posted
You can also check for NULL in your SQL SELECT statement and set a value there:
SELECT IsNull(OfficeName,'') AS OfficeName, ...
If you have access to the SQL.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 12, 2010 12:25 PM -
User-952121411 posted
Good question, and yes you must check if a value is null 1st or that exception will occur. I will give you the syntax to check for database null values using IsDbNull for pulling feilds directly out of a DataSet and onto a TextBox control. However, the source data object (DataSet) is not the focal point here, you can apply this code to other situations too.
For Each dr As DataRow In ds.Tables(0).Rows If Not IsDBNull(dr("FirstName")) Then Me.txtFirstName.Text = dr("FirstName").ToString() If Not IsDBNull(dr("LastName")) Then Me.txtLastName.Text = dr("LastName").ToString() If Not IsDBNull(dr("Phone")) Then Me.txtPhoneNum.Text = Integer.Parse(dr("FirstName")) Next
Hope this helps!- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, October 14, 2010 9:28 AM