insert query datetime input

chrisb

Member
Joined
Oct 12, 2012
Messages
10
Programming Experience
Beginner
HI i have problem in executing my insert query which returns datetime conversion error Input string was not in a correct format.


Me.ShopTableAdapter.InsertQuery1(Convert.ToDouble(TextBox3.Text), sec_no_combo.Text, ComboBox2.Text, dep_no_combo.Text, ComboBox1.Text, group_no_combo.Text, ComboBox4.Text, Convert.ToDouble(gst.Text), TextBox2.Text, Convert.ToDouble(units.Text), DateTime.Now, 0, 0, 0, sup_no_combo.Text, ComboBox3.Text, Convert.ToDouble(TextBox3.Text), Convert.ToDouble(cost_inc.Text), Convert.ToDouble(cost_ex.Text), Convert.ToDouble(rrp.Text), TextBox3.Text, Convert.ToDouble(TextBox1.Text), Convert.ToDouble(units.Text), Convert.ToDouble(sell1.Text), 0, 0, 0, TextBox2.Text, fam_no.Text, 0, 0, 0, DateTime.Now, DateTime.Now)

i was using sql server 2008.



thanks
chris
 
That means that one of your Convert.ToSomething calls is failing. It's never a good idea to convert text to a numeric type or the like like that without validating it first. A user can type anything into a TextBox so you can't assume that the data you get is correct, or even that there is data provided. You should be pre validating and converting all those inputs and notifying the user if there's an error before you get to that line. Only if all the data is valid do you then try to use it.

You can use the Double.TryParse method to both validate and convert text to a Double, so that's what you should be using. Please read the documentation first and then, if you need to, check out the multitude of examples online to see how it's done.

Also, you really should get rid of all those default names and give all your controls meaningful names. That's a great way to end up using the wring data in the wrong place.
 
Back
Top