Solved

Insert Null into DateTime column in SQL from C#

Posted on 2011-09-16
4
1,027 Views
Last Modified: 2012-05-12
trying to insert null value into datetime column in sql server but getting

Insert Unsuccessful on Record 0. Error Message The conversion of a varchar data
type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

tried DBNull.Value also couldnt :<
SqlDateTime dtLeaveEarlierBy = SqlDateTime.Null;
                                                                        sqlcmd.CommandText = "INSERT INTO StudentAttendance (attendancestatus, lateduration, actualtime, arrivedtime, leavetime, actualleavetime, leavestatus, earlierduration, absensereason, leaveearlierby, markby, datecreated, datemodified) VALUES ('" + dsSAA.Tables[0].Rows[0]["attendancestatus"].ToString() +
                                                                        "', '" + dsSAA.Tables[0].Rows[0]["lateduration"].ToString() + "', '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["actualtime"]) + "', '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["arrivedtime"]) + "', '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["leavetime"]) + "', '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["actualleavetime"]) +
                                                                        "', '" + dsSAA.Tables[0].Rows[0]["leavestatus"].ToString() + "', '" + dsSAA.Tables[0].Rows[0]["earlierduration"].ToString() + "', '" + dsSAA.Tables[0].Rows[0]["absensereason"].ToString() + "', " + dtLeaveEarlierBy + ", " + Convert.ToInt64(dsSAA.Tables[0].Rows[0]["markby"]) + ", '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["datecreated"]) +
                                                                        "', '" + Convert.ToDateTime(dsSAA.Tables[0].Rows[0]["datemodified"]) + "')";
                                                                        sqlcmd.ExecuteNonQuery();

Open in new window

0
Comment
Question by:doramail05
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
nixkuroi earned 500 total points
ID: 36553144
You should be able to just leave the column name and value out of the insert statement and it will insert a null by default.  
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 36553148
I would recommend using SqlParameters to carry out your update. They take all the hard work out of it for you.

However as you are using a string you need to use null in the string e.g.

"insert into MyTable (MyField) values (null)"

Obviously that need to fit in with the rest of your code. I don't think setting the variable to sql null as you are doing helps in this case (it would if you used an SqlParameter).  

Instead of "', " + dtLeaveEarlierBy + ", "

you want

either "', null, " or "', " + (SomeTest ? "null" : "Some Other DateTime Value") + ", "
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36553153
It is because you are passing this into a string versus using SqlParameter passed to your SqlCommand.
You can either switch to parameters and use DBNull.Value OR you can use an IF condition to concatenate "NULL" or "', " + dtLeaveEarlierBy + ", " based on condition IsDBNull(dtLeaveEarlierBy).
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 36553157
Ha. dale_burrell beat me to it. :) I would go with what he said. ;)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now