Fanan Hassan

Fanan Hassan

  • NA
  • 33
  • 646

bar code data and Update/insert

Jan 8 2018 5:54 PM
Guys i am new to C# and stuck on below problem
 
1. i am not able to update data as per below code. insert is working normally but not the update. some syntax error which i cant figure out.
 
2. barcode is populated but not able to either insert/ update. gives me error. while if input normal number from my keyboard is gets inserted in database. so why barcode is not getting checked on SELECT command.
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3. SqlConnection cn = new SqlConnection(global::Inventory_System.Properties.Settings.Default.Database1ConnectionString);  
  4. cn.Open();  
  5. SqlCommand cmdCount = new SqlCommand("SELECT count(*) from Stock WHERE ProductCode = @ProductCode", cn);  
  6. cmdCount.Parameters.AddWithValue("ProductCode", tbstockpc.Text);  
  7. int count = (int) cmdCount.ExecuteScalar();  
  8. if (count > 0)  
  9. {  
  10. try  
  11. {  
  12. int Stprice = 0;  
  13. int qty = Convert.ToInt32(tbstockqty.Text);  
  14. int puprice = Convert.ToInt32(tbstockpurprice.Text);  
  15. Stprice = qty * puprice;  
  16. string sql = "Update Stock(ProductCode,ProductDesc,Quantity,Stockdate,UnitPrice,UnitPricePur,StockPrice) values('" + tbstockpc.Text + "','" + tbstockdesc.Text + "','" + tbstockqty.Text + "','" + tbstockdate.Text + "','" + tbstocksaleprice.Text + "','" + tbstockpurprice.Text + "', '" + Stprice + "' )";  
  17. SqlCommand exeSql = new SqlCommand(sql, cn);  
  18. exeSql.ExecuteNonQuery();  
  19. this.StockTableAdapter.Update(this.database1DataSet.Stock);  
  20. MessageBox.Show("Data is Updated to Database""Stock Entered", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  21. }  
  22. catch (Exception ex)  
  23. {  
  24. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  25. }  
  26. finally  
  27. {  
  28. cn.Close();  
  29. tbstockdesc.Text = " ";  
  30. tbstockpc.Text = " ";  
  31. tbstockpurprice.Text = " ";  
  32. tbstockqty.Text = " ";  
  33. tbstocksaleprice.Text = " ";  
  34. }  
  35. }  
  36. else  
  37. {  
  38. try  
  39. {  
  40. int Stprice = 0;  
  41. int qty = Convert.ToInt32(tbstockqty.Text);  
  42. int puprice = Convert.ToInt32(tbstockpurprice.Text);  
  43. Stprice = qty * puprice;  
  44. string sql = "insert Stock(ProductCode,ProductDesc,Quantity,Stockdate,UnitPrice,UnitPricePur,StockPrice) values('" + tbstockpc.Text + "','" + tbstockdesc.Text + "','" + tbstockqty.Text + "','" + tbstockdate.Text + "','" + tbstocksaleprice.Text + "','" + tbstockpurprice.Text + "', '" + Stprice + "' )";  
  45. SqlCommand exeSql = new SqlCommand(sql, cn);  
  46. exeSql.ExecuteNonQuery();  
  47. MessageBox.Show("Data is Inserted to Database""Stock Entered", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  48. //this.stockTableAdapter.Fill(this.database1DataSet.stock);  
  49. this.StockTableAdapter.Fill(this.database1DataSet.Stock);  
  50. }  
  51. catch (Exception ex)  
  52. {  
  53. MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  54. }  
  55. finally  
  56. {  
  57. cn.Close();  
  58. tbstockdesc.Text = " ";  
  59. tbstockpc.Text = " ";  
  60. tbstockpurprice.Text = " ";  
  61. tbstockqty.Text = " ";  
  62. tbstocksaleprice.Text = " ";  
  63. }  
  64. }  
  65. }  
 
 

Answers (2)