DmO DmO - 2 years ago 86
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);

Answer Source

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

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download