up vote 1 down vote favorite

I have a simple page I'm creating that just inserts some data from textboxes in .Net using C#. I get the overflow error stating the date must be within a specific range. The text being entered in the txtBirthdate box would be something like: 01/01/1980.

When debugging, the Client1 _Birthdate object shows {1/1/1980 12:00:00}. So as far as I can tell it's doing what it's supposed to. Thanks in advance for any help.

protected void Button1_Click(object sender, EventArgs e)
{
    DataClasses1DataContext db = new DataClasses1DataContext();

    Client client1 = new Client
    {
        FirstName = txtFirstName.Text.ToString(),
        LastName = txtLastName.Text.ToString(),
        MiddleInitial = Convert.ToChar(txtMI.Text),
        Alias = txtAlias.Text.ToString(),
        Address = txtAddress.Text.ToString(),
        City = txtCity.Text.ToString(),
        State = txtState.Text.ToString(),
        Zip = Convert.ToInt32(txtZip.Text),
        Phone = txtPhone.Text.ToString(),
        Birthdate = Convert.ToDateTime(txtBirthdate.Text.ToString()),
        SSN = Convert.ToInt32(txtSSN.Text),
        DLNumber = txtDLNumber.Text.ToString(),
        Gender = Convert.ToByte(ddGender.Text),
        PrimaryRace = Convert.ToByte(ddPrimaryRace.Text),
        SecondaryRace = Convert.ToByte(ddSecondaryRace.Text),
        Ethnicity = Convert.ToByte(ddEthnicity.Text),
        Veteran = Convert.ToBoolean(ddVeteranStatus.Text),
        HoH = Convert.ToBoolean(ddHoH.Text)
    };

    db.Clients.InsertOnSubmit(client1);
    db.SubmitChanges();
}
link|flag
Have you other datetime columns in your table? – Gregoire Mar 10 at 21:03

2 Answers

up vote 0 down vote

If you are about to insert your datetime into SQL-Sever, you will probably need to check for its range because C# DateTime.MinValue is different from SQL DateTime.MinValue.

C# DateTime MinValue = 1/1/0001

SQL DateTime MinValue = 1/1/1753

Also

SQL SmallDateTime MinValue = 1/1/1900

C# DateTime is a non-nullable variable type. So make sure it's not null before inserting into your table in your DAL. If NULL, the MinValue (or whatever random value stored in memory) will be used.

link|flag
up vote 0 down vote

Gregoire was exactly right. I went back and looked and I had a datestamp but also had a default value set in SQL for getdate(). I thought I could still insert without having to pass the datestamp value. Apparently not. I opted to fill it from the C# code using the DateTime.Now which is actually better anyway!

link|flag

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.