Click here to Skip to main content
15,891,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
i want to insert null value in datetime. i am using following code but it insert as 1900-01-01 00:00:00.000


C#
con.Open();
string str="insert into table(podate) values(@PO_Date)";
cmd = new SqlCommand(str, con);
cmd.Parameters.Add(new SqlParameter("@PO_Date", SqlDbType.DateTime));
                sqldatenull = SqlDateTime.Null;
                if (PO_Date == "")
                {
                    cmd.Parameters["@PO_Date"].Value = System.Data.SqlTypes.SqlDateTime.Null;
                    //cmd.Parameters["@PO_Date"].Value = DBNull.Value;
                    //cmd.Parameters["@PO_Date"].Value = null;
                }
                else
                {
                    cmd.Parameters["@PO_Date"].Value = DateTime.Parse(PO_Date);
                }

podate insert as:1900-01-01 00:00:00.000;

pls help me.

thanking you in advance!!
Posted
Comments
Saral S Stalin 27-Mar-12 1:39am    
Can you debug your code and see if the PO_Date is coming as empty string ("") when no date is specified. If the PO_Date value is '0' it will get inserted as 1900-01-01 00:00:00.000
rajesh@1989 27-Mar-12 1:42am    
i want to insert podate=null
instead of 1900-01-01 00:00:00.000;

Allow null values in the table and do not pass any parameter while inserting values in this column.
 
Share this answer
 
Comments
Shahin Khorshidnia 27-Mar-12 5:08am    
+5
Mukund Thakker 28-Mar-12 2:09am    
Thank you
fct2004 28-Mar-12 1:05am    
This works as long at the default value hasn't been changed to something other than null.
 
Share this answer
 
Comments
Shahin Khorshidnia 27-Mar-12 5:10am    
My+5
Good Link
Abhinav S 27-Mar-12 8:43am    
Thank you.
[no name] 28-Mar-12 11:16am    
good one
Abhinav S 28-Mar-12 22:19pm    
Thank you.
in the Sql server , go to table design view then tick mark in the Allow Nulls column of podate.

this will be used to sort out your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900