0

i use this query to insert data

cmd = new OleDbCommand("insert into avoir_note  values ('" + 
       comboBoxA.Text + "','" + comboBoxCM.Text + "','" + 
       comboBoxCE.Text + "','" + textBoxMat.Text + "'," +
       Convert.ToDouble(textBoxNote.Text)+ ",'" + 
       dateTimePickerDT.Text + "') ", Program.cn) 

if i type for exemple 10 in textBoxNote it s work but

if i type for exemple 10,15 in textBoxNote i get this error The number of query values ​​must match the number of fields destination. if i type for exemple 10.15 in textBoxNote i get this error input string was not in correct format

2
  • 1
    Please use parameterized queries. This kind of string concatenations are open for SQL Injection attacks. Commented Apr 11, 2014 at 21:20
  • the are 6 fields in the table avoir_note Commented Apr 11, 2014 at 21:25

2 Answers 2

2

FIrst of all, you shouldn't concatenate the values into the query like that, you should use a parameterised query. Then you don't have those problems with formatting the values correctly.

The reason for the error message is that you are formatting the floating point number with a comma as decimal separator, but the database expects it to be a period. The comma makes it two separate values instead of one, so the number of values doesn't match the number of fields. Use a culture information that has a period as decimal separator to format the number:

Convert.ToDouble(textBoxNote.Text).ToString(CultureInfo.InvariantCulture)

That will make the code work for now, so that you can make the query parameterised in your own time. The code is wide open for SQL injection attacks, so you should definitely not release it to production in its current state.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks i am new in c# a don't know how to use culture information
0

Correct me if Im wrong, but that is 10,15 with a comma! You need to use a dot symbol '.'.

The error message means that the it expects a different number of parameters than the ones provided and the comma will add an extra parameter!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.