1

I've been building a small inventory system for my workplace and have stumbled on an error that I cannot seem to fix

 private void Update(string num,string name, string quant, string location, string category, string numquery)
    {
       // "UPDATE Inventory SET Inventorynumber='"+ num +"',Inventory_Name='"+name+"', Quantity ='"+ quant+"',Location ='"+ location+"' Category ='"+ category+"' WHERE Inventorynumber ='"+ numquery +"';";
        string query = "UPDATE Inventory SET Inventorynumber='" + Convert.ToInt16(num) + "',Inventory_Name='" + name + "', Quantity ='" + quant + "',Location ='" + location + "' Category ='" + category + "' WHERE Inventorynumber ='" + Convert.ToInt16(numquery) + "'";
        if (this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = query;
            cmd.Connection = serverconnection;
            cmd.ExecuteNonQuery();
            this.CloseConnection();
            Bind();
        }
    }

I have no idea what to change here. Any help would be appreciated.

Darkestlyrics
  • 310
  • 1
  • 5
  • 16

6 Answers6

13

Problem: You are missing the comma after location parameter in your query.
Solution: You need to separate the parameters using a comma.

Suggestion : Use parameterized queries to avoid SQL Injection Attacks.

Try this:

private void Update(string num,string name, string quant, string location, string category, string numquery)
    {
       // "UPDATE Inventory SET Inventorynumber='"+ num +"',Inventory_Name='"+name+"', Quantity ='"+ quant+"',Location ='"+ location+"' Category ='"+ category+"' WHERE Inventorynumber ='"+ numquery +"';";
        string query = "UPDATE Inventory SET Inventorynumber=@Inventorynumber,Inventory_Name=@Inventory_Name, Quantity =@Quantity ,Location =@Location,Category =@Category WHERE Inventorynumber =@Inventorynumber";
        if (this.OpenConnection() == true)
        {
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = query;
            cmd.Parameters.AddWithValue("@Inventorynumber",Convert.ToInt16(num));
            cmd.Parameters.AddWithValue("@Inventory_Name",name);
            cmd.Parameters.AddWithValue("@Quantity",quant);
            cmd.Parameters.AddWithValue("@Location",location);
            cmd.Parameters.AddWithValue("@Category",category);
            cmd.Parameters.AddWithValue("@Inventorynumber",Convert.ToInt16(numquery));
            cmd.Connection = serverconnection;
            cmd.ExecuteNonQuery();
            this.CloseConnection();
            Bind();
        }
    }
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
3

Yes the error is in the missing comma, but this is the result of all that mess with string concatenation that ends always in subtle syntax errors.
Why don't you use a parameterized query? It is a lot simpler to write and you avoid parsing errors like this and (more important) you avoid Sql Injections

private void Update(string num,string name, string quant, string location, string category, string numquery)
{
    string query = "UPDATE Inventory SET Inventorynumber=@num, Inventory_Name=@name, " +
                   "Quantity =@qty,Location =@loc, Category =@cat " + 
                   "WHERE Inventorynumber =@numquery";
    if (this.OpenConnection() == true)
    {
        MySqlCommand cmd = new MySqlCommand(query, serverconnection);
        cmd.Parameters.AddWithValue("@num", Convert.ToInt16(num));
        cmd.Parameters.AddWithValue("@name", name);
        cmd.Parameters.AddWithValue("@qty", quant);
        cmd.Parameters.AddWithValue("@loc", location);
        cmd.Parameters.AddWithValue("@cat", category);
        cmd.Parameters.AddWithValue("@numquery", Convert.ToInt16(numquery));
        cmd.ExecuteNonQuery();
        this.CloseConnection();
        Bind();
    }
}

As a side note I have some doubts about some parameters type. Are you sure that quantity is really a string as implied by the presence of quotes around your original value? Also the numquery and num variables are of type string, you try to convert then to short integer and then you put them inside quotes (meaning that in the database the fields are of type text). This makes no sense at all. If the database expects numbers then do not use quotes, if the database expects strings then do not try to convert. Another reason to use a parameterized query that force you to reflect on these issues.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • It's really my first time using this, I plan to clean it up. But for the moment we need it done quick and dirty. – Darkestlyrics Dec 10 '13 at 10:42
  • Not to argue with you, but trust me, string concatenation is NEVER the quick way to go. I agree on the dirty part though :-) – Steve Dec 10 '13 at 10:46
2

You are missing a Comma between location and category. You have heard this million times befor i know, but its really much better using prepared statements so you do not have to take care of this kind of things and your code is much more readable.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
1

You missed the comma

Location ='" + location + "', Category ='" + category + "'
//  see the `,` between Location and Category
Linga
  • 10,379
  • 10
  • 52
  • 104
1

you have missed comma(,) in query:

string query = "UPDATE Inventory SET Inventorynumber='" + Convert.ToInt16(num) + "',Inventory_Name='" + name + "', Quantity ='" + quant + "',Location ='" + location + "' Category ='" + category + "' WHERE Inventorynumber ='" + Convert.ToInt16(numquery) + "'";

Make it as:

string query = "UPDATE Inventory SET Inventorynumber='" + Convert.ToInt16(num) + "',Inventory_Name='" + name + "', Quantity ='" + quant + "',Location ='" + location + "', Category ='" + category + "' WHERE Inventorynumber ='" + Convert.ToInt16(numquery) + "'";
C Sharper
  • 8,284
  • 26
  • 88
  • 151
0

Try removing the ' single quotes around the integers?