Difference between null and System.DBNull.Value      S.No     null                                DBNull      1        Con...
instance of type object, and therefore can be assigned DBNull:Example-3If (amountValue > 0)  row["AmountValue"] = amountVa...
Note: However note that neither checking null of nor DBNull.value will work if the index is out ofrange. If we check for a...
Upcoming SlideShare
Loading in...5
×

Difference between null and system.db null.value

1,009

Published on

Difference between null and System.DBNull.Value

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,009
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
26
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Difference between null and system.db null.value

  1. 1. Difference between null and System.DBNull.Value S.No null DBNull 1 Conceptual Difference: Conceptual Difference: The keyword null represents The class System.DbNull an invalid reference. represents a nonexistent value in a database field. 2 What is the purpose of null What is the purpose of DBNull keyword ? class ? NULL is used to check if a DBNULL is used to check if a whole record is not found in certain field in a database is null. the database. 3 Is it a Value / Reference / Is it a Value / Reference / Nullable type ? Nullable type ? null is the default value of DBNull does not match the type of reference-type variables. a nullable value type, a reference Ordinary value types cannot be type, or a value type, and therefore null. is not directly assignable. Please look at: Handle null, then DBNull to avoid InvalidCastException Reason: Of DBNull class only one instance can exist as it a singleton class. Sole instance is DBNull.Value.Handle null, then DBNull to avoid InvalidCastException:For instance, sometimes in code you will see items in a data row being used withoutchecking for null first. When the underlying field is not null, this is OK, but if a null isavailable, the following code will not work:Example-1Decimal amount = (Decimal)row["AmountValue"]; //returned value is DBNullBecause DBNull is not the same type, an InvalidCastException occurs. Rather, a nullcheck has to be performed. The safe alternative to this is the following:Example-2Decimal amount = 0;If (!row.IsNull("AmountValue")) amount = (Decimal)row["AmountValue"];If you want to set a rows value, you can do something like below. The row takes an
  2. 2. instance of type object, and therefore can be assigned DBNull:Example-3If (amountValue > 0) row["AmountValue"] = amountValue;Else row["AmountValue"] = DBNull.Value;The value property returns the actual instance of DBNull (a static property). Sometimes, ahelper routine would be good to handle this conversion for you, reducing the amount to atotal of one line instead of four. That may not seem like much, but a table with thirtycolumns will make this much coding a chore.Checking null and DBNull valuesHow to check for null value ? 1. Create the variable. A variable must be declared before using it. The code belowallocates memory and declares the variable for use. string myNullVar = null; The code uses the "null" value assignment for the new variable. 2. Check if the variable is null. The code below checks if the variable is null. If it is null,the code assigns it a color value. if(myNullVar == null) { myNullVar = "purple"; } 3.Check if the variable is not null. In some cases, you may wan to reassign the variableback to null. The following code checks if the variable is not null, and assigns it a nullvalue. if(myNullVar != null) { myNullVar = null; }How to check for DBNull values ? 4. Declare a variable. Just like section 1, to check for DBNull, a variable needs to bedeclared. However, the variable is assigned the DBNull value. string myNullVar = System.DBNull; 5. Use the "Equals()" method from the string class to check for the DBNull value. Thecode below checks for this value and assigns myNullVar a color. if (myNullVar.Equals(System.DBNull.Value)) { myNullVar = "purple"; } 6.Evaluate if the variable is not null. Similar to section one, you may want to check if thevariable is not null to reassign it back to DBNull. The "!" character evaluates to "not" in thefollowing example. if(!myNullValue.Equals(System.DBNull.Value)) { myNullValue = System.DBNull; }
  3. 3. Note: However note that neither checking null of nor DBNull.value will work if the index is out ofrange. If we check for an index or a column that doesnt exist, it will throw an index out ofrange exception.References:http://stackoverflow.com/questions/4958379/what-is-the-difference-between-null-and-system-dbnull-valuehttp://www.dotnetspider.com/forum/315649-Difference-between-NULL-DBNULL.aspxhttp://aspalliance.com/1460_More_InDepth_About_Nulls_And_DBNull.4http://msdn.microsoft.com/en-us/library/system.dbnull.value.aspxhttp://msdn.microsoft.com/en-us/library/edakx9da.aspxhttp://www.ehow.com/how_5158769_check-null-value-c.htmlhttp://dotnetanalysis.blogspot.in/2012/07/c-difference-between-dbnullvalue-and.htmlAnd, further updates on difference between questions and answers, please visit myblog @ http://onlydifferencefaqs.blogspot.in/

×