Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

insert null in smalldatetime field Expand / Collapse
Author
Message
Posted Monday, March 8, 2010 12:53 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 3, 2010 5:22 AM
Points: 11, Visits: 9
Hello,
I have a webform and if user doesn't select any date from the form then it will send null in the database,i am using typed datasets.
Is there a way to store null in smalldatetime format, because when i try to do it using my code it give me exception
if (d == "")
{
d = null;
}
int i = studentTable.InsertStudent(txtName.Text, Convert.ToDateTime(d));

exception:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Post #878450
Posted Monday, March 8, 2010 1:23 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 6:09 AM
Points: 697, Visits: 356
Try To Insert Without Convert.
Post #878455
Posted Monday, March 8, 2010 2:56 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 3, 2010 5:22 AM
Points: 11, Visits: 9
studentTableAdapter studentTable = new studentTableAdapter();
string s = txtName.Text;
string d = txtDate.Text;
if (d == "")
{

d = null;


int i = studentTable.InsertStudent(txtName.Text, Convert.ToDateTime(d));
}

here's my code and i am using typed dataset in my application, without convertto.datetime it gives error that best overloaded function for insertstudent has some invalid argument(string,System.datetime)
Post #878474
Posted Monday, March 8, 2010 3:24 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Tuesday, September 23, 2014 6:09 AM
Points: 697, Visits: 356
try like this
System.Data.SqlTypes.SqlDateTime sqlDateTime = new System.Data.SqlTypes.SqlDateTime(System.DateTime.Now);

convert your date to sql date then insert.
Post #878484
Posted Monday, March 8, 2010 3:37 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, July 28, 2014 1:07 PM
Points: 579, Visits: 1,807
I am not certain about ADO.NET but should not you set
DBNULL.Value 

to your variable d?

(Sorry if I am holding the other end of the stick)


---------------------------------------------------------------------------------
Post #878489
Posted Monday, March 8, 2010 4:49 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, May 3, 2010 5:22 AM
Points: 11, Visits: 9
thanks for your replies
but i have done it by using variable of type DateTime? which allows null value.
thanks alot
Post #878519
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse