public user_buy(string str) { InitializeComponent(); //loads database in datagridview on form load SqlDataAdapter sda; DataTable dt; sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; label1.Text = str; }
//show full record of entered category private void button1_Click(object sender, EventArgs e) { SqlDataAdapter sda; DataTable dt; Connection con = new Connection(); //searches in database for specific category and display full record sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile Where (category ='" + textBox1.Text + "' )", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; //counts total mobiles in that specific category string query = " SELECT COUNT(model) FROM Mobile Where (category ='" + textBox1.Text + "' )"; SqlDataAdapter sda1 = new SqlDataAdapter(query, con.ActiveCon()); DataTable source = new DataTable(); sda1.Fill(source); label4.Text = textBox1.Text + " phones are " + source.Rows[0][0].ToString(); label3.Visible = true; }
//login button private void button2_Click(object sender, EventArgs e) { //checks if user is already present in database or not sda = new SqlDataAdapter("Select Count(*) from Login where Username = '******' and Password = '******'", con.ActiveCon()); sda.Fill(dt); //if yes then login successful if (dt.Rows[0][0].ToString() == "1") { MessageBox.Show("Login Successful."); this.Hide(); //to show user's First name and Last Name in next all forms, passing name as value to user_main form call sda1 = new SqlDataAdapter("Select First_Name, Last_Name from Login where Username = '******' and Password = '******'", con.ActiveCon()); sda1.Fill(dt1); user_main mn = new user_main((dt1.Rows[0][0].ToString() + dt1.Rows[0][1].ToString()).ToString()); mn.Show(); } //if not then login error else { MessageBox.Show("Error! Please Check your Username or Password."); } }
//Delete Button private void button5_Click(object sender, EventArgs e) { //if clicked then fetch all record against the given id and delete it. SqlCommand cmd = new SqlCommand(" DELETE From Mobile WHERE (id ='" + textBox7.Text + "' )", con.ActiveCon()); cmd.ExecuteNonQuery(); MessageBox.Show("Mobile with id: " + textBox7.Text + " is Deleted."); }
public admin_view_all() { InitializeComponent(); //loads database into datagridview at runtime Connection con = new Connection(); sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; }
private void button2_Click(object sender, EventArgs e) { //makes a new pdf document Document document = new Document(PageSize.A4, 25, 25, 30, 30); //name of document and type PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Order.pdf", FileMode.Create)); document.Open(); int num = -1; //adds first paragraph Paragraph p0 = new Paragraph(" Your Order:\n\nQuantity Model Price\n\n"); document.Add(p0); //Adds full data of shopping cart to the paragraph in pdf, quantity, model and price of every mobile foreach (DataGridViewRow item in dataGridView2.Rows) { num = item.Index; Paragraph p1 = new Paragraph(dataGridView2.Rows[num].Cells[2].Value.ToString() + " " + dataGridView2.Rows[num].Cells[1].Value.ToString() + " " + dataGridView2.Rows[num].Cells[3].Value.ToString()); document.Add(p1); } //decrements quantity of every mobile which are checked out int count = -1; foreach (DataGridViewRow qntity in dataGridView2.Rows) { count = qntity.Index; Connection con = new Connection(); SqlCommand cmd = new SqlCommand("UPDATE Mobile SET quantity = quantity - '" + dataGridView2.Rows[count].Cells[2].Value.ToString() + "' WHERE (model = '" + dataGridView2.Rows[count].Cells[1].Value.ToString() + "')", con.ActiveCon()); cmd.ExecuteNonQuery(); } int error_count = -1; //checks if quantity of a mobile which is decremented becomes less than 0 than, then gives stock error and order is cancelled foreach (DataGridViewRow qntity in dataGridView2.Rows) { error_count = qntity.Index; using (SqlCommand cmd = new SqlCommand("select quantity from Mobile WHERE (model = '" + dataGridView2.Rows[error_count].Cells[1].Value.ToString() + "')", con.ActiveCon())) { //Execute the query and just get the first result. int value = (int)cmd.ExecuteScalar(); if (value < 0) { //if quantity < 0, then error MessageBox.Show("Error!!! Out of Stock."); Connection conn = new Connection(); SqlCommand cmd1 = new SqlCommand("UPDATE Mobile SET quantity = quantity + '" + dataGridView2.Rows[error_count].Cells[2].Value.ToString() + "' WHERE (model = '" + dataGridView2.Rows[error_count].Cells[1].Value.ToString() + "')", conn.ActiveCon()); cmd1.ExecuteNonQuery(); Application.Exit(); } else { //if quantity is ok, then adds total price to the pdf Paragraph p2 = new Paragraph("\n Total Price: Rs. " + label13.Text); document.Add(p2); document.Close(); writer.Close(); //Opens the pdf file //You have to set its path locally //As @"Drive\..\Visual Studio\Projects\App1\bin\Debug\Order.pdf" //i just gave a rough sample System.Diagnostics.Process.Start(@"C:\Users\Faiq\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Order.pdf"); } } } }
private void button5_Click(object sender, EventArgs e) { //Makes a new data adapter to check if input mobile exists in database or not. SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from Mobile where category = '" + textBox2.Text + "' and model = '" + textBox3.Text + "'", con.ActiveCon()); DataTable dt = new DataTable(); //fills datatable sda.Fill(dt); if (dt.Rows[0][0].ToString() == "1") { //if exists then add 1 in the quantity of that mobile SqlCommand cmd = new SqlCommand("UPDATE Mobile SET quantity = quantity + 1 WHERE (category = '" + textBox2.Text + "' and model = '" + textBox3.Text + "')", con.ActiveCon()); cmd.ExecuteNonQuery(); MessageBox.Show("Mobile is Already Present, so quantity of " + textBox2.Text + " " + textBox3.Text + " is increased"); } else { //if not exist then add a new row in the table SqlCommand cmd = new SqlCommand(" INSERT INTO Mobile(category, model, price) VALUES('" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox6.Text + "')", con.ActiveCon()); cmd.ExecuteNonQuery(); MessageBox.Show("Mobile Successfully Added."); } }
//show full record of entered model private void button2_Click_1(object sender, EventArgs e) { SqlDataAdapter sda; DataTable dt; Connection con = new Connection(); //searches in database for specific model and display full record sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile Where (model ='" + textBox2.Text + "' )", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView2.DataSource = dt; using (SqlCommand cmd = new SqlCommand("select quantity from Mobile WHERE (model = '" + textBox2.Text + "')", con.ActiveCon())) { //Execute the query and just get the first result. int value = (int)cmd.ExecuteScalar(); label5.Text = textBox2.Text + " phones are " + value; label6.Visible = true; } }
//refeshes the loaded database at runtime to see updated values private void button1_Click(object sender, EventArgs e) { Connection con = new Connection(); sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; }
//updates textboxes value in database against their own specific id private void button5_Click(object sender, EventArgs e) { Connection con = new Connection(); SqlCommand cmd = new SqlCommand(" UPDATE Mobile SET category = '" + textBox6.Text + "', model = '" + textBox1.Text + "', quantity = '" + textBox2.Text + "', price = '" + textBox3.Text + "' WHERE (id ='" + textBox7.Text + "' )", con.ActiveCon()); cmd.ExecuteNonQuery(); scb = new SqlCommandBuilder(sda); sda.Update(dt); }
public admin_update_manual() { InitializeComponent(); //loads database on form load Connection con = new Connection(); sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; }
public admin_update_from_table() { InitializeComponent(); //makes a new connection Connection con = new Connection(); //fills whole database in datagridview on load sda = new SqlDataAdapter(" SELECT id, category, model, quantity, price FROM Mobile", con.ActiveCon()); dt = new DataTable(); sda.Fill(dt); dataGridView1.DataSource = dt; }