0
votes

I am getting an error while updating the data, the error says: "Syntax error (missing operator) in query expression '[Code] IN('."

Here is the code where i am getting an error at:

OleDbDataReader dReader = cmd.ExecuteReader();

Here is the full code of the function that the error is appear:

private void UpdateQuantity()
        {
            int index = 0;

            string command = "UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN(";

            OleDbConnection conn = new OleDbConnection(connectionString);

            conn.Open();

            OleDbCommand cmd = new OleDbCommand(command, conn);

            cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);

            OleDbDataReader dReader = cmd.ExecuteReader();

            while (dReader.Read())
            {
                if (textBoxQuantityContainer[index].Value != 0 && textBoxQuantityContainer[index].Value >= Convert.ToDecimal(dReader["Quantity"].ToString()))
                {
                    newVal = Convert.ToDecimal(dReader["Quantity"].ToString()) - textBoxQuantityContainer[index].Value;
                }

                System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
                sound.Play();
                MessageBox.Show("Updated Successfully", "Success");

                index += 1;
            }

            conn.Close();
            dReader.Close();
        }

Here is the previous code where the query expression "Code IN":

string query = "SELECT [Quantity], [Description], [Price] FROM [Seranne] WHERE [Code] IN (";

                OleDbConnection conn = new OleDbConnection(connectionString);

                conn.Open();

                if (int.TryParse(this.textBoxCodeContainer[0].Text, out codeValue))
                {
                    query = query + codeValue.ToString();
                }

                for (int i = 1; i < 17; i++)
                {
                    if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
                    {
                        query = query + "," + codeValue.ToString();
                    }
                }

                query = query + ")";

                OleDbCommand cmd = new OleDbCommand(query, conn);

                cmd.Parameters.Add("Code", System.Data.OleDb.OleDbType.Integer);
                cmd.Parameters.Add("Quantity", System.Data.OleDb.OleDbType.Integer);

                OleDbDataReader dReader;

                dReader = cmd.ExecuteReader();

                while (dReader.Read())
                {
                    if (textBoxCodeContainer[index].TextLength != 0)
                    {
                        this.textBoxQuantityContainer[index].Maximum = Convert.ToDecimal(dReader["Quantity"].ToString());
                        this.textBoxDescContainer[index].Text = dReader["Description"].ToString();
                        this.textBoxSubTotalContainer[index].Text = dReader["Price"].ToString();
                    }

                    index += 1;
                }

                conn.Close();
                dReader.Close();

Thanks a bunch!

2
your query isn't complete string command = "UPDATE [Seranne] SET [Quantity] ='" + newVal + "' WHERE [Code] IN(";Charaf JRA

2 Answers

2
votes

OK, just keep your code and change this:

for (int i = 1; i < 17; i++) {
   if (int.TryParse(this.textBoxCodeContainer[i].Text, out codeValue))
   {
      query = query + codeValue + ",";
   }
}
query = query.TrimEnd(',') + ")";
1
votes

before proceed you better create integer list out of textBoxCodeContainer values, say it as integers

then

string command = "UPDATE [Seranne] SET [Quantity] =? WHERE [Code] IN(" + string.Join(", " , integers) +")";

Generate integer list as below

List<int> integers = new List<int>();
foreach (var tb in textBoxCodeContainer)
{
    int codeValue;
    if (int.TryParse(tb.Text, out codeValue))
    {
        integers.Add(codeValue);
    }
}