.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Working with Null Data Fields

Posted By:Shashi Ray       Posted Date: November 29, 2009    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  


Working with Null Data Fields

Here are a few tips to help you correctly use null field values in the .NET Data Architecture:

  • Always set the value of a null field using the System.DBNull class. Do not use the null value provided by C# or Visual Basic .NET. For example:
·         rowStudents["Nickname"] = DBNull.Value   // correct!
  • Strongly typed DataSets contain two additional methods per DataRow-one to check if a column contains a null value, and another to set the column value to null. These are shown in the following code fragment:
·         If (tds.rowStudent[0].IsPhoneNoNull()) {..}
·         tds.rowStudent[0].SetPhoneNoNull()
  • Always use the IsNull method of the DataRow class (or the strongly typed equivalent given in the previous bullet) to test for null values from a database. This method is the only supported way to test for null database values.
  • If it is possible that a data field contains a null value, be sure to test for it (with the IsNull method) before using the value in a context that expects a non-null value. A typical example of this is Integer-valued data fields that may be null. Note that the .NET run time Integer datatype does not include the null value. Here is an example:
·         int i = rowStudent["ZipCode"];         // throws exception if null!
  • Use the nullValue annotation of a strongly typed DataSet .xsd file to configure how null values from a database are mapped. By default, an exception is thrown; however, for more fine-grained control, you can configure the class to replace the null value with a specified value, such as String.Empty.



Shashi Ray

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend