Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert Null into DateTime column in SQL from C#

Posted on 2011-09-16
4
Medium Priority
?
1,121 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 2000 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 60

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 60

Expert Comment

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

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

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.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Integration Management Part 2
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

824 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