DmO DmO - 7 months ago 10
SQL Question

Insert date from a textbox into sql(database)

I'm using only date in my sql database. But when i insert example: 10-5-2016 it saves like 5-10-2016. How can i change that?

Sqlcommand cmd = new Sqlcommand(" insert into MemberForm (registrationdate)
values '" + textBox1.Text + "'",con);
cmd.ExecuteNonQuery();

Answer

There might be a problem with internationalization (most countries use day-month-year instead of month-day-year, cause its obv way better). Check your db server settings.

When inserting dates as string, you have to use "yyyy-MM-dd" cause thats the format accepted, check it here http://www.w3schools.com/sql/sql_dates.asp.

this might work:

 DateTime dt = new DateTime();
 dt = Convert.ToDateTime(TextBox1.Text);
 string date1 = dt.ToString("yyyy-MM-dd");

 sqlcommand cmd = new sqlcommand(" insert into MemberForm (registrationdate)   
 values '" + date1 + "'",con);
 cmd.ExecuteNonQuery();
Comments