Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello everyone,
I am getting a error when updating customer details.The error is
"SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM."
The code is working correctly in local host but is showing error in server.Please help me to resolve this.Thanks in advance.
The code i have used for DOB and Anniversary is

C#
var dateString = txtDOB.Text;
           var fr = new System.Globalization.CultureInfo("en-US");
           var resultingDate = DateTime.ParseExact(dateString, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);

           stuCA.DOB = DateTime.Parse(resultingDate.ToString(fr));

           var dateString1 = txtAnnDate.Text;
           var fr1 = new System.Globalization.CultureInfo("en-US");
           var resultingDate1 = DateTime.ParseExact(dateString1, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);

           stuCA.ANNIVERSARY = DateTime.Parse(resultingDate1.ToString(fr1));
Posted

1 solution

You need to look at how you are passing the data to SQL - since it is SQL that is complaining and the C# code you show which should generate the DateTime objects will not throw SQL exceptions.

So, start with whatever code you have passing the DOB and ANNIVERSARY values to SQL, and check that they use parametrised queries - I'm betting they don't, and the problem is that the string version generated in production and passed into the SQL Command string is a different format to teh development version.
 
Share this answer
 
Comments
Member 10194425 10-Feb-14 4:49am    
parametrised queries means what..??in what format should i pass it to SQL.Can u share code?
OriginalGriff 10-Feb-14 4:58am    
Parameterised queries mean using SqlParameter objects instead of concatenating strings - and if you don't use them all the time then your database is at severe risk of SQL Injection Attack - Google for "Bobby Tables" and don't assume it's just a joke.

using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
{
com.Parameters.AddWithValue("@C1", myValueForColumn1);
com.Parameters.AddWithValue("@C2", myValueForColumn2);
com.ExecuteNonQuery();
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900