Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

<pre lang="cs">private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand(&quot;INSERT INTO Login(Admin,Password,Age,Gender,Dateofbirth)VALUES(&#39;&quot; + textBox1.Text + &quot;&#39;,&#39;&quot; + textBox2.Text + &quot;&#39;,&#39;&quot;+textBox3.Text+&quot;&#39;,&#39;&quot;+ Gender +&quot;&#39;,&#39;&quot;+this.dateTimePicker1.Value.ToString(&quot;dd/mm/yyyy hh:mm&quot;) +&quot;&#39;)&quot;, con);
            cmd.ExecuteNonQuery();
            con.Close();
            MessageBox.Show(&quot;data Registered successfully!&quot;);
        }</pre>
Posted
Comments
Your code got messed up. Please edit it again.

The error could be generated if your SQL server is using a current culture that is incompatible with the culture info from your string format. For example the Server where your SQL is running is using default culture info "en-US" and that means the date is in the next format: "mm/dd/yyyy" and you are sending from your code the date string in the German format "de-DE" format: "dd/mm/yyyy".

If you are using values like "07/23/2014" ==> out of range, because 23 is not a valid month as SQL server will expect for that position!

So the solution is to send the date strings from your C# code to the SQL server in the format that the SQL Server is expected, in your case should be "mm/dd/yyyy".

PS: Similar problem could be generated also for numerical values conversion from strings!
 
Share this answer
 
v3
 
Share this answer
 

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