Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Insert Null into DateTime column in SQL from C#

Posted on 2011-09-16
4
1,050 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

860 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