I am not able to insert date from text box which is in DD/MM/YYYY format. I am using Visual Studio 2008 on Windows 7.
my language and regional settings are "ENGLISH(United Kingdom)", and my system is showing today's date like 12/12/2013. The same date i have get in text box via form On load event.
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = DateTime.Now.ToShortDateString();
}
which results into 12/12/2013.
I have placed one button to store date in Table "Table1" which contain only one field Date and its data type is DATETIME. The one click event contains following lines.
SqlConnection c = new SqlConnection();
c.ConnectionString ="Data Source=.\\SQLEXPRESS;AttachDbFilename='D:\\Documents\\Visual Studio 2008\\Projects\\test\\test\\Database1.mdf';Integrated Security=True;User Instance=True";
c.Open();
DateTime d;
d = Convert.ToDateTime(textBox1.Text);
string q = "insert into table1 values('" + d + "')";
SqlCommand cmd = new SqlCommand(q, c);
cmd.ExecuteNonQuery();
MessageBox.Show("record inserted");
Whenever i edit date Say today is 12/12/2013(DD/MM/YYYY) and i typed 13/12/2013 , it shows the error Sql exception was unhandeled.
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated.
and when i type 12/13/2013 (MM/DD/YYYY)
it shows String was not recognized as a valid DateTime.
Any Suggestion Friends..???