Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am creating windows application using c# 2010, here i am using data grid view for billing purpose, once i am stored the values to database, and then after some time i am retrieve my first bill values and i am doing some changes and update the records for same bill number, but that time error is came.

this is for my update code


C#
using (SqlConnection connect = new SqlConnection(db.Connectionstring()))
          {
              using (SqlCommand command = new SqlCommand())
              {
                  string pro = "";
                  int qty;
                  int totqty;
                  command.Connection = connect;


                  command.CommandText = "update bill set staffid=staffid' and cstid='" + txtmobile.Text + "' and cstname='" + txtname.Text + "' and schemes='" + txtscheme2.Text + "' and counum='" + txtnum.Text + "' and date='" + txtdate.Text + "' and slno=slno, proname=proname, price=price, qty=qty, protax=protax, dis=dis, amt=amt where(recnum=@recnum)";










                  command.Parameters.Add(new SqlParameter("@staffid", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@cstid", SqlDbType.Int));
                  command.Parameters.Add(new SqlParameter("@cstname", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@schemes", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@counum", SqlDbType.VarChar));



                  command.Parameters.Add(new SqlParameter("@recnum", SqlDbType.Int));
                  command.Parameters.Add(new SqlParameter("@date", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@slno", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@proname", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@price", SqlDbType.Float));
                  command.Parameters.Add(new SqlParameter("@qty", SqlDbType.VarChar));
                  command.Parameters.Add(new SqlParameter("@protax", SqlDbType.Float));
                  command.Parameters.Add(new SqlParameter("@dis", SqlDbType.Float));
                  command.Parameters.Add(new SqlParameter("@amt", SqlDbType.Float));

                  command.Parameters.Add(new SqlParameter("@totamt", SqlDbType.Float));
                  command.Parameters.Add(new SqlParameter("@amtdis", SqlDbType.Float));
                  command.Parameters.Add(new SqlParameter("@finamt", SqlDbType.Float));


                  connect.Open();
                  foreach (DataGridViewRow row in dgvfind.Rows)
                  {
                      if (!row.IsNewRow)
                      {
                          command.Parameters["@staffid"].Value = txtstaff.Text;
                          command.Parameters["@cstid"].Value = txtmobile.Text;
                          command.Parameters["@cstname"].Value = txtname.Text;
                          command.Parameters["@schemes"].Value = txtscheme2.Text;
                          command.Parameters["@counum"].Value = txtnum.Text;

                          command.Parameters["@recnum"].Value = lblbill.Text;
                          command.Parameters["@date"].Value = txtdate.Text;



                          command.Parameters["@slno"].Value = row.Cells[0].Value;
                          command.Parameters["@proname"].Value = row.Cells[1].Value;
                          pro = row.Cells[1].Value.ToString();
                          command.Parameters["@price"].Value = row.Cells[2].Value;
                          command.Parameters["@qty"].Value = row.Cells[3].Value;
                          qty = Convert.ToInt32(row.Cells[3].Value);
                          command.Parameters["@protax"].Value = row.Cells[4].Value;
                          command.Parameters["@dis"].Value = row.Cells[5].Value;
                          command.Parameters["@amt"].Value = row.Cells[6].Value;

                          command.Parameters["@totamt"].Value = txt_netamount.Text;
                          command.Parameters["@amtdis"].Value = textBox3.Text;
                          command.Parameters["@finamt"].Value = textBox2.Text;



                         command.ExecuteNonQuery();


But error is came

C#
Incorrect syntax near ' and cstid='.
Unclosed quotation mark after the character string ' and slno=slno, proname=proname, price=price, qty=qty, protax=protax, dis=dis, amt=amt where(recnum=@recnum)'.


How to solve above error any one give me ideas.

What I have tried:

Incorrect syntax near Unclosed quotation mark after the character string
Posted
Updated 29-Jul-16 0:36am

1 solution

Well yes - your quotation marks don't make any sense!
C#
command.CommandText = "update bill set staffid=staffid' and cstid='"
But then, the whole SQL is wrong, and even if it wasn't it would bne extremely dangerous. 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.

You've even got the code in there to add the parameters, but you don't use them at all!
Try:
C#
command.CommandText = "update bill set staffid=@staffid, cstid=@cstid, ...
And provide the parameters properly:
C#
command.Parameters.AddWithValue("@staffid", txtstaff.Text);
...

But do yourself (and your users) a favour: some of those are numeric values, so verify and convert the user inputs to the appropriate numeric value first before you try to pass them to SQL by using int.TryParse, double.TryParse, and so forth. If there is a problem, report it to the user instead of letting bad data to to the DB and hoping SQL rejects it.
 
Share this answer
 
v2
Comments
Boopalslm 29-Jul-16 12:41pm    
your code is working good, but how to now update, i am adding new one row for data grid view now how to store same bill number for my new row.

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