Click here to Skip to main content
15,032,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using the following code in save button of c# windows application.

cmd = new SqlCommand("insert into sman_targets(sman_cd,from_dt,to_dt,target_amt) values('"+comboBox1.Text +"','"+DT1.Text+"','"+DT2.Text+"','"+textBox2.Text+"')",con);


DT1,DT2 are datetime picker controls in (short format) in c# windows form.

I have used "datetime" datatime for the table columns in sql server database.

on button click it shows the following errors. It seems some problem is there
with data formats.

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

plz give the solution.

thanks and regards


What I have tried:

I tried by changing the double quotes / singles quotes of insert statement etc
also by changing the format property of datatime picker control.
But the same error is appearing.
Updated 10-Aug-18 3:41am
Richard MacCutchan 10-Aug-18 5:30am
You are taking the Text value of the datepicker and trying to post it as a datetime type. Do what OriginalGriff advises and get the proper values and types first and pass them as parameter values, not as text.

Start by not doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
A perfectly valid "delete the table" command
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Fix that throughout your whole app, validate user inputs (and convert them to appropriate .NET data types) and your problem will disappear at the same time. Rather than your database.
cmd = new SqlCommand("insert into sman_targets(sman_cd,from_dt,to_dt,target_amt) values('"+comboBox1.Text +"','"+DT1.Text+"','"+DT2.Text+"','"+textBox2.Text+"')",con);

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
After you go through and read how to properly create an SQL statement; you should end up with something similar to the following protocode. You may need to need/want to work with the variables going into to trim spaces and validate the DateTime value.

cmd = new SqlCommand("INSERT sman_targets(sman_cd,from_dt,to_dt,target_amt) VALUES (@comboBox1, @DT1, @DT2, @textBox2)", con);

cmd.Parameters.AddWithValue("@comboBox1", comboBox1.Text);
cmd.Parameters.AddWithValue("@DT1", (DateTime)DT1.Text);
cmd.Parameters.AddWithValue("@DT2", (DateTime)DT2.Text);
cmd.Parameters.AddWithValue("@textBox2", textBox2.Text);


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