Solved

Insert Null into DateTime column in SQL from C#

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

16 Experts available now in Live!

Get 1:1 Help Now