Handling Database NULL in .Net while using SqlParameter (.Net 2.0)

No.of Views1126
Bookmarked0 times
Downloads 
Votes0
By  ayyanarj   On  16 Feb 2010 03:02:22
Tag : CSharp , How to
Handling Database NULL in .Net while using SqlParameter (.Net 2.0)
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

 Introduction

 If you want to set the NULL for database column while inserting or updating, normal “null” OR “DBNULL.value” is not suitable.

Code Snippet

param[0] = new SqlParameter("@MiddleName", SqlDbType.VarChar , 50);
param[0].Value = null;

param[0] = new SqlParameter("@MiddleName", SqlDbType.VarChar , 50);
param[0].Value = DBNULL.value;


If you are using Sqlparameter for passing the parameter value to stored procedure, the above null types does not support. It will throw an error like “Parameter @MiddleName not specified.”.

Code Snippet

using System.Data.SqlTypes;
param[0] = new SqlParameter("@MiddleName", SqlDbType.VarChar , 50);
param[0].Value = SqlString.Null;

 
The above code will execute properly without any error.

The same way you have to use for other data types like
DataTime --> SqlDateTime.Null
int32 --> SqlInt32.Null, etc

Conclusion

When you are call stored procedure in .NET you have to take care always corect data type and size.or else there is alternative method SqlCommand.Parametets.AddWithValue(name.value) will able to use without specify the size or type.

 
Sign Up to vote for this article
 
About Author
 
ayyanarj
Occupation-Software Engineer
Company-Effindi Technologies Private Limited
Member Type-Junior
Location-India
Joined date-04 Aug 2009
Home Page-http://ayyanar.blogspot.com
Blog Page-http://ayyanar.blogspot.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top