Thursday, June 23, 2011

Interesting Facts about DateTime and Varchar/Nvarchar in c# vs DataBase

When we insert data from either a console or ASP.net app to DataBase there are some interesting pit falls in a way Data types are handled from both ends.
We know that string in c# accepts null/empty values and you also have designed in the DB whether as Varchar/Nvarchar and allowing null.
When we insert value in the  DB it will not put null instead it throws error .So the best way to do is
Ex:
cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = DBNull.Value;
Similarly for the DateTime
 cmd.Parameters.Add("@OrgTargetDate", SqlDbType.DateTime).Value = System.Data.SqlTypes.SqlDateTime.Null; 

No comments:

Post a Comment