2
Answers

bar code data and Update/insert

Photo of Fanan Hassan

Fanan Hassan

7y
236
1
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)

2
Photo of Ramesh Palanivel
NA 9.5k 138.7k 7y
Hi fanan,
 
Your update query is wrong, Thats the reason you updating failed. Please try with below code,
 
  1. string sql = "Update Stock set ProductCode='" + tbstockpc.Text + "',ProductDesc='" + tbstockdesc.Text + "',Quantity='" + tbstockqty.Text + "',Stockdate='" + tbstockdate.Text + "',UnitPrice='" + tbstocksaleprice.Text + "',UnitPricePur='" + tbstockpurprice.Text + "',StockPrice='" + Stprice + "'";  
 
 
Accepted
1
Photo of Fanan Hassan
NA 17 281 7y
Thanks alot buddy. this worked.
 
Now one more thing is that barcodes are different and as above code i tried using biginteger as some of the bar codes exeeds the int range. tired biginteger but its giving me error. i have added the reference dll and using system numerics. 
on int below is error
 
The conversion of the nvarchar value '8964000977736' overflowed an int column.
 
changed int to  biginteger in code and  (changed in SQL table also)
Specified cast is not valid.