Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables Stocks table and Sales table
I want to subtract the quantity in Stock with the Quantity in Sales table.
After subtraction Stocks table should be Updated with the new value


The Function I created is not working, it is giving two error messages;
1. Block 1: SQL logic error incomplete input
2. SQL logic error incomplete input

What I have tried:

C#
private void updateStock()
         {
            int newAmount = 0;
             try
             {
                SetConnection();
                sql_con.Open();
                sql_cmd = sql_con.CreateCommand();
                
                string CommandText = "SELECT total_in_stock FROM Stocks_record WHERE item_code = " + txtProCode.Text;
                DB5 = new SQLiteDataAdapter(CommandText, sql_con);
                //DS5.Reset();

                try
                {
                    DB5.Fill(DT5);
                    foreach (DataRow row in DT5.Rows)
                    {
                        newAmount = int.Parse(row["total_in_stock"].ToString()) - int.Parse(txtQuantity.Text);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Block 1: " + e.Message);
                }

                string query = "UPDATE Stocks_record SET total_in_stock = @i WHERE item_code = " + txtProCode.Text;
                SQLiteCommand cmd = new SQLiteCommand(query, sql_con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@i", newAmount);
                cmd.ExecuteNonQuery();
                DB5 = new SQLiteDataAdapter(CommandText, sql_con);
                DB5.Fill(DT5);

                //DT5 = DS5.Tables[0];
                dataGridView2.Refresh();
                dataGridView2.DataSource = DT5;
                sql_con.Close();
            }

             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
Posted
Updated 8-Apr-19 18:31pm
v2

1 solution

The error itself clears the confusion, SQL logic error incomplete input. Did you try to quote the variables in your content? As far as I know, SQL requires that you use string quotation around the data, like,
SQL
... WHERE data = 'something'
Any SQL guru can correct me if I am wrong here. So, it might be that your query is incomplete and causing this issue.

Lastly, do not use concatenation in SQL queries as this exposes the queries to SQL Injection, always consider using parameters to pass the input to queries. You are already doing that in the UPDATE query, but incompletely.

SQL injection - Wikipedia[^]
 
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