Introduction
Inserting a null value to the DateTime Field in SQL Server is one of the most common issues giving various errors. Even if one enters null values the value in the database is some default value as 1/1/1900 12:00:00 AM.
The Output of entering the null DateTime based on the code would in most cases have errors as:
- String was not recognized as a valid DateTime.
- Value of type 'System.DBNull' cannot be converted to 'String'.
Or no error but DataTime entered in Database would be as 1/1/1900 12:00:00 AM
So lets write the code to enter null values in the DataBase.
The user interface is as follows:

Namespaces used
- System.Data.SqlClient/ System.Data.OleDb
- System.Data.SqlTypes
- Code for System.Data.SqlClient
C#
- string sqlStmt;
- string conString;
- SqlConnection cn = null;
- SqlCommand cmd = null;
- SqlDateTime sqldatenull;
- try {
- sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date) ";
- conString = "server=localhost;database=Northwind;uid=sa;pwd=;";
- cn = new SqlConnection(conString);
- cmd = new SqlCommand(sqlStmt, cn);
- cmd.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.NVarChar, 11));
- cmd.Parameters.Add(new SqlParameter("@LastName", SqlDbType.NVarChar, 40));
- cmd.Parameters.Add(new SqlParameter("@Date", SqlDbType.DateTime));
- sqldatenull = SqlDateTime.Null;
- cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
- cmd.Parameters["@LastName"].Value = txtLastName.Text;
- if (txtDate.Text == "") {
- cmd.Parameters["@Date"].Value = sqldatenull;
- //cmd.Parameters["@Date"].Value = DBNull.Value;
- }
- else {
- cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
- }
- cn.Open();
- cmd.ExecuteNonQuery();
- Label1.Text = "Record Inserted Succesfully";
- }
- catch(Exception ex) {
- Label1.Text = ex.Message;
- }
- finally {
- cn.Close();
- }
- Dim sqlStmt As String
- Dim conString As String
- Dim cn As SqlConnection
- Dim cmd As SqlCommand
- Dim sqldatenull As SqlDateTime
- Try
- sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (@FirstName,@LastName,@Date) "
- conString = "server=localhost;database=Northwind;uid=sa;pwd=;"
- cn =New SqlConnection(conString)
- cmd =New SqlCommand(sqlStmt, cn)
- cmd.Parameters.Add(New SqlParameter("@FirstName", SqlDbType.NVarChar, 11))
- cmd.Parameters.Add(New SqlParameter("@LastName", SqlDbType.NVarChar, 40))cmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime))
- sqldatenull = SqlDateTime.Null
- cmd.Parameters("@FirstName").Value = txtFirstName.Text
- cmd.Parameters("@LastName").Value = txtLastName.Text
- If (txtDate.Text = "") Then
- cmd.Parameters("@Date").Value = sqldatenull
- 'cmd.Parameters("@Date").Value = DBNull.Value
- Else
- cmd.Parameters("@Date").Value = DateTime.Parse(txtDate.Text)
- End If
- cn.Open()
- cmd.ExecuteNonQuery()
- Label1.Text = "Record Inserted Succesfully"
- Catch ex As Exception
- Label1.Text = ex.Message
- Finally
- cn.Close()
- End Try
C#
- string sqlStmt;
- string conString;
- OleDbConnection cn = null;
- OleDbCommand cmd = null;
- try {
- sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?) ";
- conString = "Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data source=localhost";
- cn = new OleDbConnection(conString);
- cmd = new OleDbCommand(sqlStmt, cn);
- cmd.Parameters.Add(new OleDbParameter("@FirstName", OleDbType.VarChar, 40));
- cmd.Parameters.Add(new OleDbParameter("@LastName", OleDbType.VarChar, 40));
- cmd.Parameters.Add(new OleDbParameter("@Date", OleDbType.Date));
- cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
- cmd.Parameters["@LastName"].Value = txtLastName.Text;
- if ((txtDate.Text == "")) {
- cmd.Parameters["@Date"].Value = DBNull.Value;
- }
- else {
- cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
- }
- cn.Open();
- cmd.ExecuteNonQuery();
- Label1.Text = "Record Inserted Succesfully";
- }
- catch(Exception ex) {
- Label1.Text = ex.Message;
- }
- finally {
- cn.Close();
- }
- Dim sqlStmt As String
- Dim conString As String
- Dim cn As OleDbConnection
- Dim cmd As OleDbCommand
- Try
- sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?) "
- conString = "Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data source=localhost"
- cn =New OleDbConnection(conString)
- cmd =New OleDbCommand(sqlStmt, cn)
- cmd.Parameters.Add(New OleDbParameter("@FirstName", OleDbType.VarChar, 40))cmd.Parameters.Add(New OleDbParameter("@LastName", OleDbType.VarChar, 40))cmd.Parameters.Add(New OleDbParameter("@Date", OleDbType.Date))cmd.Parameters("@FirstName").Value = txtFirstName.Text
- cmd.Parameters("@LastName").Value = txtLastName.Text
- If (txtDate.Text = "") Then
- cmd.Parameters("@Date").Value = DBNull.Value
- Else
- cmd.Parameters("@Date").Value = DateTime.Parse(txtDate.Text)
- End If
- cn.Open()
- cmd.ExecuteNonQuery()
- Label1.Text = "Record Inserted Succesfully"
- Catch ex As Exception
- Label1.Text = ex.Message
- Finally
- cn.Close()
- End Try

Ramesh PalaniappanPosted Aug 29, 2016, 2:37 AM
Good one
kalu singh raoPosted Jul 9, 2016, 12:24 PM
Nice...
bnsb chqbookPosted Feb 8, 2016, 7:34 PM
Wow Sooooooooooooo Many Thanks.It is very useful. I tried to solve this type of query but i could not get any solutions since a long time and now i finally get it
Amir ChabokPosted Mar 17, 2012, 3:37 AM
thanks a lot dear
jimmy shownPosted Mar 5, 2011, 8:59 AM
I am unable to add null or Insert record using this code..Please, help... Lettle help would much appreciate. I am new to asp.net also... Dim sqlStmt As String Dim conString As String Dim cn As OracleConnection Dim cmd As OracleCommand Dim sqldatenull As OracleDate Try sqlStmt = "insert into temp (startdate, enddate) Values (@startdate, @enddate)" 'conString = "server=localhost;database=Northwind;uid=sa;pwd=;" conString = "Data Source=abc; User Id=abc;Password=ab" cn = New OracleConnection(conString) cmd = New OracleCommand(sqlStmt, cn) 'cmd.Parameters.Add(New sqlParameter("@FirstName", SqlDbType.NVarChar, 11)) 'cmd.Parameters.Add(New SqlParameter("@LastName", SqlDbType.NVarChar, 40)) 'cmd.Parameters.Add(New SqlParameter("@Date", SqlDbType.DateTime)) cmd.Parameters.Add(New OracleParameter("@startdate", OracleDbType.TimeStamp)) cmd.Parameters.Add(New OracleParameter("@enddate", OracleDbType.TimeStamp)) sqldatenull = OracleDate.Null 'cmd.Parameters("@FirstName").Value = txtFirstName.Text 'cmd.Parameters("@LastName").Value = txtLastName.Text If (txtEndDate.Text = "") Then cmd.Parameters("@EndDate").Value = sqldatenull 'cmd.Parameters("@Date").Value = DBNull.Value Else cmd.Parameters("@EndDate").Value = DateTime.Parse(txtEndDate.Text) End If cn.Open() cmd.ExecuteNonQuery() 'Label1.Text = "Record Inserted Succesfully" Catch ex As Exception 'Label1.Text = ex.Message Finally 'cn.Close() End Try
Sajjad AhmedPosted Aug 25, 2010, 4:57 AM
Hello Sushila Patel Thx for share his knowledge which is very helpful for me to solve his problem after lot time to wast to search about that. Sajjad Ahmed Software Developer ITBeams(Pvt) Ltd. Lahore Pakistan
VijayPosted Nov 26, 2009, 1:14 AM
It's handy.
rajithaPosted Oct 22, 2009, 10:02 PM
Hi This article helped me alot. I was trying to update the details by passing parameters to a stored procedure. I couldn't find a solution to pass null value to a stored procedure. This code helped in solving my problem. Thanks.
VikramPosted Oct 14, 2009, 8:47 AM
Hi, I used above example in my application. I can insert null in my Datetime column. But now I want query my table against Date column, so how i compare Datetime with NULL. I tried by comparing directly with NULL it doesn't work. Any other way to achieve this. Thanks
Daniel PatfieldPosted Oct 31, 2007, 11:27 AM
I was able to produce code that would NULL a full datetime if a user didn't enter a date, but what if I've broken up my datetime field into two controls (a date and a time control) and they enter a date but no time? Currently I get for example: 1/10/2007 00:00:00.000, but when they save the data and come back to the form, the time is no longer NULL or empty, but rather 12:00 AM. How can I get it to STAY empty (NULL)? I currently use another field in conjuction, called NULLTime (ex: AdmitDateTime and AdmitNULLTime) if the user enters no time, then the NULLTime field is hidden and checked, but indicates that even though the datetime field is showing 12:00 AM, don't show it in the interfaces or reports. I'd rather have a more simple answer, however. I user VB.NET by the way. My email is [email protected]