Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All
I developed and created setup.exe a Windows Based Application. After installing the software in client machine I open the Order Form and input some data to it and clicked submit button to insert data into database. It throws an exception as like:-
Error:- System.Data.SqlClient.SqlException: The conversion of a char data type to a datetime datatype resulted in an out-of-range datetime value.
The statement has been terminated.


<img src="C/Users/Mashkoor/Desktop/untitled.jpg" />;
While I used datetime datatype for 'Date of Order' column in SQL Server 2005 database. In windows application I used a datetimepicker control for 'Date of Order'column. The date format in the Client Machine is like 11 September 2013


[edit]
Sir, this is the code snippet which I wrote to insert data:

C#
try
{
    string s = "insert into Sales_Details values('" + comboBox1.Text + "','" + comboBox2.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','"+dateTimePicker1.Value+"','"+dateTimePicker2.Value+"','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')";
    SqlConnection con = new SqlConnection(Class1.cs);
    con.Open();
    SqlCommand cmd = new SqlCommand(s, con);
    cmd.ExecuteNonQuery();
    MessageBox.Show("Information Saved Successfully");
    textBox3.Text = "";
    comboBox2.Text = "";
    textBox1.Text = "";
    textBox4.Text = "";
    textBox2.Text = "";
    dateTimePicker1.Text = "";
    dateTimePicker2.Text = "";
    textBox5.Text = "";
    textBox6.Text = "";
    textBox7.Text = "";
    textBox8.Text = "";
    con.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error:"+ex);
}
Posted
Updated 11-Sep-13 0:34am
v3
Comments
Richard MacCutchan 11-Sep-13 5:15am    
What is the actual SQL statement, and what are the values of the parameters that it is using?
Md M Ahmad 11-Sep-13 6:26am    
Comment moved to OP question.
Richard MacCutchan 11-Sep-13 6:37am    
Yet another example of bad coding practices. You are not validating any of your inputs. You are not checking the return value from the SQL command - you just assume it succeeds. And your code is wide open to SQL injection attacks.
Rewrite it with proper checks and use parameterized SQL queries.
Md M Ahmad 11-Sep-13 6:50am    
Sir I am very new to C#, and very curious to know things well, I don't know how to protect codes from SQL injection attacks. Please help me to write good codes. As I given some code snippet please correct it by using parametrized queries. Thanks
Richard MacCutchan 11-Sep-13 12:04pm    
The subject is covered in http://csharp-station.com/Tutorial/AdoDotNet/Lesson06. I would suggest you work through all the lessons in this excellent tutorial.

So, if you are using a DateTimePicker, why are you converting it to a string to pass it to SQL?

Simple: because your code is wide open to SQL injection attack because you are concatenating strings to form an SQL command instead of passing them as parametrized queries and sending the values in a sensible, unconverted form.
So when you write
C#
string cmd = "INSERT INTO MyTable (dateColumn) VALUES ('" + myDateTimePicker.Value + "')";
The string presented to SQL depends on the system date and time format settings in the PC running the C# code: it could be
SQL
INSERT INTO MyTable (dateColumn) VALUES ('11 September 2013')
Or
SQL
INSERT INTO MyTable (dateColumn) VALUES ('11 09 2013')
Or
SQL
INSERT INTO MyTable (dateColumn) VALUES ('2013/9/11')
Since SQL normally expects:
SQL
INSERT INTO MyTable (dateColumn) VALUES ('2013-09-11')
there is a good chance it will complain on some PCs and not on others.
Use a parametrized query, and pass DateTime values as DateTimes, integers as integers, etc.
 
Share this answer
 
Comments
Md M Ahmad 11-Sep-13 6:19am    
If I will use this string "INSERT INTO MyTable (dateColumn) VALUES ('11 September 2013')", it will insert only 11 September 2013 into the table.Other date would be not inserted.
OriginalGriff 11-Sep-13 6:24am    
Did you read what I said, at all?

Pay attention to the bits that say "Use a parametrized query"...
Md M Ahmad 11-Sep-13 7:44am    
Thank You Sir..............problem has been solve by using parametrized queries.
OriginalGriff 11-Sep-13 8:33am    
You're welcome!
Md M Ahmad 11-Sep-13 6:38am    
Sorry Sir, I could not understand at all. Please give an example.
Use DateTime type to get the date from client machine.
 
Share this answer
 
Comments
Md M Ahmad 11-Sep-13 6:27am    
Sir, already I have used that kind of datatype.
change the date format in client machine


go to control panel in the double clink on regional and language option select the time format to English(united kingdom)

i think this will slove u r issue
 
Share this answer
 
Comments
lukeer 11-Sep-13 8:17am    
You forgot to place the joke icon.
OriginalGriff 11-Sep-13 8:33am    
I don't think he's joking... :sigh:

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