Anda di halaman 1dari 10

using using using using using using using using

System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms;

namespace Forms2demo { public enum OPRType { NEW, UPT } public partial class Form1 : Form { string cat; string name, id; double price; int quan; //static List<Product> prod=new List<Product>(); public Form1() { InitializeComponent(); } OPRType op; private void groupBox1_Enter(object sender, EventArgs e) { } private void radioButtonElectronics_CheckedChanged(object sender, EventA rgs e) { if (radioButtonGrocery.Checked) cat = radioButtonGrocery.Text; else if (radioButtonCloths.Checked) cat = radioButtonCloths.Text; else if (radioButtonElectronics.Checked) cat = radioButtonElectronics.Text; else if (radioButtonToys.Checked) cat = radioButtonToys.Text; else if (radioButtonHealth.Checked) cat = radioButtonHealth.Text; else if (radioButtonAutomobiles.Checked)

cat = radioButtonAutomobiles.Text; } private void buttonDeatil_Click(object sender, EventArgs e) { //Product p = new Product(id, name, cat, quan, price); } public void Grp1(bool t) { groupBox2.Enabled = t; Save.Enabled = t; Cancel.Enabled = t; } public void Grp2(bool t) { New.Enabled = t; Delete.Enabled = t; Find.Enabled = t; Update.Enabled = t; } public void resetData() { textBoxPrice.Text = ""; textBoxQuantity.Text=""; textBoxName.Text = ""; radioButtonGrocery.Checked=false; radioButtonHealth.Checked = false; radioButtonElectronics.Checked = false; radioButtonToys.Checked = false; radioButtonCloths.Checked = false; radioButtonAutomobiles.Checked = false; } private void Form1_Load(object sender, EventArgs e) { Grp1(false); Grp2(true); dataGridView1.DataSource = ProdOpr.RetrieveProducts(); } private void New_Click(object sender, EventArgs e) { Grp1(true); Grp2(false); resetData(); textboxID.Text = ProdOpr.GenerateId().ToString(); op = OPRType.NEW; } private void Cancel_Click(object sender, EventArgs e)

{ Grp1(false); Grp2(true); } private void Save_Click(object sender, EventArgs e) { id = textboxID.Text; name = textBoxName.Text; quan = int.Parse(textBoxQuantity.Text); price = double.Parse(textBoxPrice.Text);

Product p = new Product(id, name, cat, quan, price); // Product p=new Product{ProductId=textboxID.Text,Name= if (op == OPRType.NEW) { ProdOpr.AddProduct(p); } else { if (ProdOpr.UpdateProduct(textboxID.Text, p)) { MessageBox.Show("Record updated Successfully"); } else { MessageBox.Show("Record not found"); } } Grp1(false); Grp2(true); dataGridView1.DataSource=ProdOpr.RetrieveProducts(); } private void textBox1_TextChanged(object sender, EventArgs e) { //delid = textBox1.Text; } private void Delete_Click(object sender, EventArgs e) { if (ProdOpr.DeleteProduct(textboxID.Text)) { MessageBox.Show("Product deleted successfully"); } else { MessageBox.Show("Product not found"); } dataGridView1.DataSource = ProdOpr.RetrieveProducts(); } private void textBox2_TextChanged(object sender, EventArgs e) {

//finid = textBox2.Text; } private void Find_Click(object sender, EventArgs e) { InputDialog id = new InputDialog(); DialogResult dr = id.ShowDialog(); if (dr == System.Windows.Forms.DialogResult.OK) { Product p = ProdOpr.FindProduct(id.textBox1.Text); if (p == null) { MessageBox.Show("Record not found"); } else { textboxID.Text = p.ProductId.ToString(); textBoxName.Text = p.Name.ToString(); textBoxPrice.Text = p.Price.ToString(); textBoxQuantity.Text = p.Quantity.ToString(); setcategory(p.category.ToString()); } } //public staic int pid=0; // dataGridView1.DataSource=ProdOpr.FindProduct(textboxID); } private void Update_Click(object sender, EventArgs e) { Grp1(true); Grp2(false); op = OPRType.UPT; } private void dataGridView1_RowEnter(object sender, DataGridViewCellEvent Args e) { if (dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) { textboxID.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.T oString(); textBoxName.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value .ToString(); textBoxPrice.Text = dataGridView1.Rows[e.RowIndex].Cells[3].Valu e.ToString(); setcategory(dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToStri ng()); textBoxQuantity.Text = dataGridView1.Rows[e.RowIndex].Cells[4].V alue.ToString(); } } public void setcategory(string cat1) { if (cat1 == radioButtonAutomobiles.Text) {

radioButtonAutomobiles.Checked = true; } if (cat1 == radioButtonCloths.Text) { radioButtonCloths.Checked = true; } if (cat1 == radioButtonToys.Text) { radioButtonToys.Checked = true; } if (cat1 == radioButtonGrocery.Text) { radioButtonGrocery.Checked = true; } if (cat1 == radioButtonElectronics.Text) { radioButtonElectronics.Checked = true; } if (cat1 == radioButtonHealth.Text) { radioButtonHealth.Checked = true; } } public static string pid = ""; private void btndosale_Click(object sender, EventArgs e) { SalesTransaction st = new SalesTransaction(); pid = textboxID.Text; st.ShowDialog(); } } } ----------------------------------------------------------------------------------------------------------------using using using using using using using System; System.Collections.Generic; System.Linq; System.Text; System.Data.SqlClient; System.Configuration; System.Data;

namespace Forms2demo { class ProdOpr { public static SqlConnection con; static ProdOpr() {

con = new SqlConnection(ConfigurationManager.ConnectionStrings["Invc on"].ConnectionString); } public static void AddProduct(Product p) { SqlCommand cmd = new SqlCommand("insert into ProductInfo values (@Id ,@Name,@Category,@Price,@Quantity)", con); cmd.Parameters.Add(new SqlParameter("@Id", p.ProductId)); cmd.Parameters.Add(new SqlParameter("@Name", p.Name)); cmd.Parameters.Add(new SqlParameter("@Category", p.category)); cmd.Parameters.Add(new SqlParameter("@Price", p.Price)); cmd.Parameters.Add(new SqlParameter("@Quantity", p.Quantity)); try { con.Open(); cmd.ExecuteNonQuery(); } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } } public static DataTable RetrieveProducts() { SqlCommand cmd = new SqlCommand("select *from ProductInfo", con); DataTable dt = new DataTable(); SqlDataReader dr; try { con.Open(); dr = cmd.ExecuteReader(); dt.Load(dr); } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } return dt; } public static Product FindProduct(string id) { SqlCommand cmd = new SqlCommand("select * from ProductInfo where Id=

@id", con); cmd.Parameters.Add(new SqlParameter("@id", id)); SqlDataReader dr; try { con.Open(); dr = cmd.ExecuteReader(); if (dr.HasRows) { dr.Read(); return new Product { ProductId = dr.GetString(0), Name = dr. GetString(1), category = dr.GetString(2), Price = dr.GetDouble(3), Quantity =dr.GetInt32(4)}; } else { return null; } } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } }

} public static bool DeleteProduct(string id) { SqlCommand cmd = new SqlCommand("delete from ProductInfo where id=@i d", con); cmd.Parameters.Add(new SqlParameter("@Id", id)); try { con.Open(); int i = cmd.ExecuteNonQuery(); if (i > 0) { return true; } else { return false; }

} catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } }

public static bool UpdateProduct(string id, Product p) { SqlCommand cmd = new SqlCommand("update ProductInfo set Name=@Name,Category=@Category,Price=@Price,Quantity=@Quantity where Id=@id" , con); cmd.Parameters.Add(new SqlParameter("@Name", p.Name)); cmd.Parameters.Add(new SqlParameter("@Category", p.category)); cmd.Parameters.Add(new SqlParameter("@Price", p.Price)); cmd.Parameters.Add(new SqlParameter("@Quantity", p.Quantity)); cmd.Parameters.Add(new SqlParameter("@Id", id)); try { con.Open(); int i = cmd.ExecuteNonQuery(); if (i > 0) { return true; } else { return false; } } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } }

public static int GenerateId() { SqlCommand cmd = new SqlCommand("select max(id) from ProductInfo", c on); DataTable dt = new DataTable(); try { con.Open(); object data = cmd.ExecuteScalar(); try { return int.Parse(data.ToString()) + 1; } catch(FormatException ex) { return 1; } } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } } } } ----------------------------------------------------------------------------------------------------------------using using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient; System.Configuration;

namespace Forms2demo { public partial class SalesTransaction : Form { public SalesTransaction() { InitializeComponent(); } private void SalesTransaction_Load(object sender, EventArgs e) {

pid.Text = Form1.pid.ToString(); } private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.Connectio nStrings["InvCon"].ConnectionString); SqlCommand cmd = new SqlCommand("doSale", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(new SqlParameter("@tid", Tid.Text)); cmd.Parameters.Add(new SqlParameter("@date", DateTime.Now)); cmd.Parameters.Add(new SqlParameter("@pid", pid.Text)); cmd.Parameters.Add(new SqlParameter("@qty", quan.Text)); cmd.Parameters.Add(new SqlParameter("@cname", cust.Text)); cmd.Parameters.Add("@status", SqlDbType.VarChar,200); cmd.Parameters[5].Direction = ParameterDirection.Output; try { con.Open(); cmd.ExecuteNonQuery(); MessageBox.Show(cmd.Parameters[5].Value.ToString()); } catch (SqlException ex) { throw ex; } finally { if (con.State != System.Data.ConnectionState.Closed) { con.Close(); } } } } }