insert null in smalldatetime field

  • 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.

  • Try To Insert Without Convert.

  • 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)

  • try like this

    System.Data.SqlTypes.SqlDateTime sqlDateTime = new System.Data.SqlTypes.SqlDateTime(System.DateTime.Now);

    convert your date to sql date then insert.

  • 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)

    ---------------------------------------------------------------------------------

  • thanks for your replies

    but i have done it by using variable of type DateTime? which allows null value.

    thanks alot

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply