-4

I have an application that I'm trying to use to make some database updates. However, whenever I try to save, I get this error message:

Incorrect Syntax Near '1'

What does this error message mean, and how can I solve it? Here's my code:

try
{
    if (dgvStockEntry.Rows.Count > 0)
    {
        if (MessageBox.Show("Are you sure you want to save this records?", stitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            for (int i = 0; i < dgvStockEntry.Rows.Count; i++)
            {
                cn.Open();
                cm = new SqlCommand("Update Products set Quantity = Quantity '" + dgvStockEntry.Rows[i].Cells[5].Value.ToString() + "' where Pcode like '" + dgvStockEntry.Rows[i].Cells[2].Value.ToString() + "'", cn);
                cm.ExecuteNonQuery();
                cn.Close();

                cn.Open();
                cm = new SqlCommand("Update StockEntry set Quantity = Quantity '" + dgvStockEntry.Rows[i].Cells[5].Value.ToString() + ", Status = 'Done' where Id like '" + dgvStockEntry.Rows[i].Cells[0].Value.ToString() + "'", cn);
                cm.ExecuteNonQuery();
                cn.Close();
            }
            Clear();
            LoadStockEntry();
        }
    }
}
catch (Exception ex)
{
    cn.Close();
    MessageBox.Show(ex.Message, stitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
} 
Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • 1
    Inspect `cm` in the debugger before the call to `ExecuteNonQuery`, but to me, the `set Quantity = Quantity ` bit looks odd. – 500 - Internal Server Error May 03 '22 at 17:32
  • 5
    This part of your SQL doesn't make sense `Quantity = Quanity 'somethinghere'` Also consider using parameters instead of injecting SQL directly. – juharr May 03 '22 at 17:32
  • 1
    The tag `sql` says: "...and a tag for the DBMS implementation (e.g. MySQL, PostgreSQL, Oracle, MS SQL Server, IBM DB2, etc.) being used." **Please correct the tags** and, please, **complete** error message would be nice too. – Luuk May 03 '22 at 17:36
  • 6
    **Always use parameterized sql and avoid string concatenation** to add values to sql statements. This mitigates SQL Injection vulnerabilities and ensures values are passed to the statement correctly. See [How can I add user-supplied input to an SQL statement?](https://stackoverflow.com/q/35163361/1260204), and [Exploits of a Mom](https://xkcd.com/327/). – Igor May 03 '22 at 17:36
  • Does this answer your question? [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements) – Charlieface May 03 '22 at 22:36
  • When you do parameterize this (which you **must**), consider using a Table-Value Parameter in order to do the whole update in one shot. Also, you must dispose your connection and command with `using`, do not cache them – Charlieface May 03 '22 at 22:38

3 Answers3

1

Well apart from the comments made by others like it is better to use parametrized queries, and you should not use like without wildcards, it is obvious that the current queries cannot work.

Your code to generate the first query:

cm = new SqlCommand("Update Products set Quantity = Quantity '" + dgvStockEntry.Rows[i].Cells[5].Value.ToString() + "' where Pcode like '" + dgvStockEntry.Rows[i].Cells[2].Value.ToString() + "'", cn);
                cm.ExecuteNonQuery();

This would evaluate to something like

Update Products set Quantity = Quantity '1' where Pcode like 'SomeCode'

which is invalid sql.

Correct would be

Update Products set Quantity = 1 where Pcode like 'SomeCode'

or

Update Products set Quantity = '1' where Pcode like 'SomeCode'

or

Update Products set Quantity = Quantity + 1 where Pcode like 'SomeCode'

so you would have to update your code to make it output something like this (I don't know the exact requirement and column data type.)

GWR
  • 204
  • 1
  • 5
-1

You can debug and run your SQL query in SQL management studio to check what is wrong with your query.

try
        {
            if (dgvStockEntry.Rows.Count > 0)
            {
                if (MessageBox.Show("Are you sure you want to save this records?", stitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    for (int i = 0; i < dgvStockEntry.Rows.Count; i++)
                    {
                        cn.Open();   

                        var quantity = dgvStockEntry.Rows[i].Cells[5].Value.ToString();
                        var pcode = dgvStockEntry.Rows[i].Cells[2].Value.ToString();
                        var sql = $"Update Products set Quantity = '{stockeEntry}' where Pcode like 'pcode'"
                        cm = new SqlCommand(, cn);
                        cm.ExecuteNonQuery();
                     
                        cm = new SqlCommand("Update StockEntry set Quantity = Quantity '" + dgvStockEntry.Rows[i].Cells[5].Value.ToString() + ", Status = 'Done' where Id like '" + dgvStockEntry.Rows[i].Cells[0].Value.ToString() + "'", cn);
                        cm.ExecuteNonQuery();
                        cn.Close();
                    }
                    Clear();
                    LoadStockEntry();
                }
                
                
            }
        }
        catch (Exception ex)
        {
            cn.Close();
            MessageBox.Show(ex.Message, stitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        } 
ramdev
  • 137
  • 1
  • 9
-1

You can try something like this with multiple added considerations.

try
{
    if (dgvStockEntry.Rows.Count > 0)
    {
        if (MessageBox.Show("Are you sure you want to save this records?", stitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            for (int i = 0; i < dgvStockEntry.Rows.Count; i++)
            {
                cn.Open();
                cm = new SqlCommand("Update Products set Quantity = @quantity where Pcode = @pcode", cn);
                SqlParameter[] paramarray = 
                    {
                        new SqlParameter("@quantity", SqlDbType.Varchar) {Value = dgvStockEntry.Rows[i].Cells[5].Value.ToString()},
                        new SqlParameter("@pcode", SqlDbType.Varchar) {Value = dgvStockEntry.Rows[i].Cells[2].Value.ToString()}
                    };
                cm.Parameters.AddRange(paramarray);
                cm.ExecuteNonQuery();
                cn.Close();

                cn.Open();
                cm = new SqlCommand("Update StockEntry set Quantity = @quantity, Status = 'Done' where Id = @id", cn);
                paramarray.clear();
                cm.Parameters.Clear();
                paramarray = 
                    {
                        new SqlParameter("@quantity", SqlDbType.Varchar) {Value = dgvStockEntry.Rows[i].Cells[5].Value.ToString()},
                        new SqlParameter("@id", SqlDbType.Varchar) {Value = dgvStockEntry.Rows[i].Cells[0].Value.ToString()}
                    };                
                cm.Parameters.AddRange(paramarray);
                cm.ExecuteNonQuery();
                cn.Close();
            }
            Clear();
            LoadStockEntry();
        }
    }
}
catch (Exception ex)
{
    cn.Close();
    MessageBox.Show(ex.Message, stitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
} 
  • Use parametrized queries
  • Don't use like if the right side of the expression do not contain a wilcard (%)
  • You don't need to open/close your connection twice, you can use the same connection to run multiple commands.
Mauricio Atanache
  • 2,424
  • 1
  • 13
  • 18