private void DeleteButton_Click(object sender, System.EventArgs e) { try { if (ProductnameComboBox.SelectedItem.ToString().Length > 0) { metroGrid1.DataSource = d = StockModel.Delete(ProductnameComboBox.SelectedItem.ToString()); } else { MessageBox.Show("Field is blank", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch { MessageBox.Show("Field is blank", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } Comborefresh(); Cleardata(); }
private void UpdateButton_Click(object sender, System.EventArgs e) { try { if (QuantityTextBox.Text.Length > 0 && ProductnameComboBox.SelectedIndex != -1) { metroGrid1.DataSource = StockModel.Update(ProductnameComboBox.SelectedItem.ToString(), Convert.ToInt32(QuantityTextBox.Text)); } else { MessageBox.Show("Field is blank", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch { MessageBox.Show("Field is blank", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } Cleardata(); Comborefresh(); }
public Stock() { InitializeComponent(); metroGrid1.DataSource = d = StockModel.Read(); }
private void RefreshButton_Click(object sender, System.EventArgs e) { metroGrid1.DataSource = d = StockModel.Read(); Comborefresh(); }
public static DataTable Add(string Productcode, string ProductName, int quantity, int price, string filename, string filepath) { try { con.Open(); trans = con.BeginTransaction(); if (filename == String.Empty) { cmd = new SqlCommand(@"insert into Products(productcode,productname,quantity,price) values('" + Productcode + "','" + ProductName + "'," + quantity + "," + price + ")", con, trans); result = cmd.ExecuteNonQuery(); if (result > 0) { if (StockModel.Add(ProductName, quantity, price)) { trans.Commit(); MessageBox.Show("..Add Successfully..", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("Error occured", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); trans.Rollback(); } } else { MessageBox.Show("Error occured", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); trans.Rollback(); } } else { string filename2 = filename.Substring(0, filename.LastIndexOf('.')); string extension = filename.Substring(filename.LastIndexOf('.'), filename.Length - 1); filename = filename2 + DateTime.Now.Date.ToString("MM/d/h/mm/yy") + "." + extension; MessageBox.Show(filename); cmd = new SqlCommand(@"insert into Products(productcode,productname,quantity,price,files) values('" + Productcode + "','" + ProductName + "'," + quantity + "," + price + ",'" + filename + "' )", con, trans); if (!System.IO.Directory.Exists(TargetPath)) { System.IO.Directory.CreateDirectory(TargetPath); } result = cmd.ExecuteNonQuery(); if (result > 0) { MessageBox.Show("..Add Successfully..", "Info", MessageBoxButtons.OK); string destFile = System.IO.Path.Combine(TargetPath, filename); System.IO.File.Copy(filepath, destFile, true); if (StockModel.Add(ProductName, quantity, price)) { trans.Commit(); } else { trans.Rollback(); MessageBox.Show("Error occured", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("Error occured", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); trans.Rollback(); } } s = new SqlDataAdapter(@"Select id as ID,productcode as ProductCode,productname as ProductName,quantity as Quantity,price as Price,(quantity*price) as Total,files as Files from Products", con); d = new DataTable(); s.Fill(d); return(d); } catch (Exception e) { MessageBox.Show(e.Message, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error); trans.Rollback(); return(null); } finally { con.Close(); } }