Click here to Skip to main content
15,916,601 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello dudes

I have this code
It does not say 'There are not enough items in stock'
I cant understood where is problem.
please help
Im using c#
SqlCommand cmd1 = new SqlCommand("IF (Select UnitsInStock from Products WHERE BarCode=@BarCode) > @Quantity BEGIN UPDATE Products SET UnitsInStock=(UnitsInStock-@Quantity) WHERE BarCode=@BarCode END ELSE BEGIN PRINT 'There are not enough items in stock'END", new SqlConnection(Program.ConnectionString));
            cmd1.Connection.Open();
            for (int i = 0; i < dtbProducts.Rows.Count; i++)
            {
                cmd1.Parameters.AddWithValue("Quantity", (int)dtbProducts.Rows[i]["Quantity"]);
                cmd1.Parameters.AddWithValue("BarCode", dtbProducts.Rows[i]["BarCode"].ToString());
                cmd1.ExecuteNonQuery();
                cmd1.Parameters.Clear();
            }

            cmd1.Connection.Close();
            cmd.Connection.Close();
            this.Close();

        }
Posted

1. You should have used Stored Procedure for this kind of operation instead of a direct query.

2. In your query, the else part says 'There are not enough items in stock'. Have you checked that based on the quantity and barcode value, which part of If is executing?

3. Your first step should be to run the defined query directly in SQL and see the results for given few parameters. Once that is working then use it in your query.

4. You execute your command in a loop without storing the result set returned in anything. I doubt that is intended! Currently, your code just shows you what is the result for the last iteration. Format and fine tune your code, check and store needed values. Use VS DEBUGGER, it will help!
 
Share this answer
 
Comments
Dalek Dave 25-Oct-10 5:47am    
Good Answer.
Use following statement
SELECT 'There are not enough items in stock'
 
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