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.
- private void button1_Click(object sender, EventArgs e)
- {
- SqlConnection cn = new SqlConnection(global::Inventory_System.Properties.Settings.Default.Database1ConnectionString);
- cn.Open();
- SqlCommand cmdCount = new SqlCommand("SELECT count(*) from Stock WHERE ProductCode = @ProductCode", cn);
- cmdCount.Parameters.AddWithValue("ProductCode", tbstockpc.Text);
- int count = (int) cmdCount.ExecuteScalar();
- if (count > 0)
- {
- try
- {
- int Stprice = 0;
- int qty = Convert.ToInt32(tbstockqty.Text);
- int puprice = Convert.ToInt32(tbstockpurprice.Text);
- Stprice = qty * puprice;
- 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 + "' )";
- SqlCommand exeSql = new SqlCommand(sql, cn);
- exeSql.ExecuteNonQuery();
- this.StockTableAdapter.Update(this.database1DataSet.Stock);
- MessageBox.Show("Data is Updated to Database", "Stock Entered", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- cn.Close();
- tbstockdesc.Text = " ";
- tbstockpc.Text = " ";
- tbstockpurprice.Text = " ";
- tbstockqty.Text = " ";
- tbstocksaleprice.Text = " ";
- }
- }
- else
- {
- try
- {
- int Stprice = 0;
- int qty = Convert.ToInt32(tbstockqty.Text);
- int puprice = Convert.ToInt32(tbstockpurprice.Text);
- Stprice = qty * puprice;
- 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 + "' )";
- SqlCommand exeSql = new SqlCommand(sql, cn);
- exeSql.ExecuteNonQuery();
- MessageBox.Show("Data is Inserted to Database", "Stock Entered", MessageBoxButtons.OK, MessageBoxIcon.Information);
-
- this.StockTableAdapter.Fill(this.database1DataSet.Stock);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- cn.Close();
- tbstockdesc.Text = " ";
- tbstockpc.Text = " ";
- tbstockpurprice.Text = " ";
- tbstockqty.Text = " ";
- tbstocksaleprice.Text = " ";
- }
- }
- }