Carol Cashman

Carol Cashman

  • NA
  • 74
  • 11.7k

Error when trying reduce a certain number from the database

Jan 8 2018 9:11 AM
hey guys,
 
I am creating a Materials resource planning system for an admin to be able to keep track of the materials needed, used etc.
 
When I receive an order via the phone the admin will enter in the size of the product they want and the amount of boxes needed (20 items per box).
 
This will then calculate all the materials needed to make the order (eg: small Jars, Large Jars, lids and stickers).
 
I am looking to find a solution to when the order is put through the system to deduct these materials needed to complete this order, to make sure the stock levels are kept up to date and do not run out of materials.
 
Therefore i am looking for a way to take the figure that shows up in a label on a web form away from the database level which is stored in a table called StockTable.
 
I have this code only for one material (stickers) to see if it worked, which it didnt.
 
this is my code:
  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3. //Creating a connection to my database using the connection string  
  4. string cs = System.Configuration.ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;  
  5. SqlConnection con = new SqlConnection(cs);  
  6. //preparing a query which will delete a row in the database based on the id that is entered  
  7. SqlCommand cmd = new SqlCommand("UPDATE StockTable SET AmtOfStickers-'" + txtStickers.Text, con);  
  8. con.Open();  
  9. cmd.ExecuteNonQuery();  
  10. con.Close();  
  11. Response.Redirect("stock.aspx");  
  12. }  
  13. }  
this is the error i am getting:
 
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
 
Additional information: ExecuteNonQuery: Connection property has not been initialized.
any advice is greatly appriciated.

Answers (4)