Hi all i am trying to insert a string of data into an sql database table from a number of textboxes, however the date value 17/07/2011 in my textbox4 is not being inserted and haulting the whole processes.
Below is the code i am using
string insertString = "insert into Herdregistar(month,uic,motheruic,mother,name,animalsex,tagno,status,owner,presence,year,wkno,day,date) values ('" + textBox6.Text + "','" + textBox11.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox12.Text + "','" + comboBox3.Text + "','" + textBox13.Text + "','" + comboBox1.Text + "','" + textBox15.Text + "','" + comboBox4.Text + "','" + textBox7.Text + "','" + textBox5.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"SqlCommand
cmd.ExecuteNonQuery();
cmd = new SqlCommand(insertString, conn);in my sql database i have set the value of the date column as "Date"
your kind advice will be highly appreciated.
Best regards
Loading

VulpesPosted Jul 17, 2011, 5:28 AM
Mike JonsonPosted Jul 18, 2011, 7:23 AM
Marvin kakuruPosted Jul 18, 2011, 1:54 AM
Thanks again men.
Zoran HorvatPosted Jul 17, 2011, 6:25 PM
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy-MM-dd";
In that case, your code would look like this:
string insertString = "insert into Herdregistar(month,uic,motheruic,mother,name,animalsex,tagno,status,owner,presence,year,wkno,day,date) values ('" + textBox6.Text + "','" + textBox11.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox12.Text + "','" + comboBox3.Text + "','" + textBox13.Text + "','" + comboBox1.Text + "','" + textBox15.Text + "','" + comboBox4.Text + "','" + textBox7.Text + "','" +textBox5.Text + "','" + textBox3.Text + "','" + "{d '" +dateTimePicker1.Text + "'})"
Zoran
Zoran HorvatPosted Jul 17, 2011, 6:06 PM
Sutish is right - you should not write hard-coded queries but rather make them with bind variables. That saves you from conversion problems and from SQL injection attacks.
Now, if you want your query to be formatted as a hard-coded query, i.e. to have constant date value, then you should better use date literal format which is accepted by MSSQL: {d 'yyyy-mm-dd'}. For example:
CREATE TABLE SimpleTable(DateColumn DATE)
INSERT INTO SimpleTable(DateColumn) VALUES({d '2011-07-16'})
This should be guaranteed to work fine on MSSQL.
Now I suppose that textBox4 contains full date in your case. Then make sure that date is contained in format yyyy-mm-dd, and then your code should be modified like this:
string insertString = "insert into Herdregistar(month,uic,motheruic,mother,name,animalsex,tagno,status,owner,presence,year,wkno,day,date) values ('" + textBox6.Text + "','" + textBox11.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox12.Text + "','" + comboBox3.Text + "','" + textBox13.Text + "','" + comboBox1.Text + "','" + textBox15.Text + "','" + comboBox4.Text + "','" + textBox7.Text + "','" +textBox5.Text + "','" + textBox3.Text + "','" + "{d '" + textBox4.Text + "'})"
And again - this is very bad method of dealing with queries and you should learn to use bind variables as soon as you can.
Zoran
Suthish NairPosted Jul 17, 2011, 4:17 PM
Guest UserPosted Jul 17, 2011, 10:57 AM