Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I have a dataGrid that I should insert its columns values to an access database ...

but I have problem with " command.ExecuteNonQuery();"

my project is not finished just for this error ... please help if you can . it's my code :
  for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++)
            {

 string query = @"INSERT INTO tbl_RentFactor 

([ID],DateNow,customerName,objectName,objectNumber,unitCost,objectCost,paidMoney,restOfMonyy,customerID,DateBack)
 VALUES

("+ID+",'" + lbldate.Text + "','" + cmdCustomName.Text + "'," + dataGridFactorRent.Rows[i].Cells[1].Value + ",
 " + dataGridFactorRent.Rows[i].Cells[3].Value + ",
" + dataGridFactorRent.Rows[i].Cells[4].Value + ",
" + dataGridFactorRent.Rows[i].Cells[5].Value + ",
'" + txtPaid.Text + "','" + lblRemained.Text + "',
"+customerID+",'"+lbldate.Text+"')";

                //try
                //{

                    con.Open();
                      command.CommandText =query;
                    command.ExecuteNonQuery();

                    con.Close();

               //} 
               // catch (Exception ex) { MessageBox.Show("error"); }
            }
Posted
Comments
Check if all the values are getting properly or not by debugger.
And check the query by debugger before executing, if it is correct or not.

1 solution

Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, and your problem will probably disappear at the same time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900