Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


insert null in smalldatetime field


insert null in smalldatetime field

Author
Message
soulreader7
soulreader7
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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.
SQL Editor
SQL Editor
Say Hey Kid
Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)

Group: General Forum Members
Points: 698 Visits: 363
Try To Insert Without Convert.
soulreader7
soulreader7
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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)
SQL Editor
SQL Editor
Say Hey Kid
Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)Say Hey Kid (698 reputation)

Group: General Forum Members
Points: 698 Visits: 363
try like this
System.Data.SqlTypes.SqlDateTime sqlDateTime = new System.Data.SqlTypes.SqlDateTime(System.DateTime.Now);

convert your date to sql date then insert.
Nabha
Nabha
Mr or Mrs. 500
Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)Mr or Mrs. 500 (587 reputation)

Group: General Forum Members
Points: 587 Visits: 1814
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)

---------------------------------------------------------------------------------
soulreader7
soulreader7
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search