You are on page 1of 5

Form1.

cs
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;

namespace assignment3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (comboBox1.SelectedItem.ToString() == "insert") { textBox1.Visible = true; textBox2.Visible = true;

textBox3.Visible = true; textBox4.Visible = true; label3.Visible = true; label4.Visible = true; label5.Visible = true; label6.Visible = true; comboBox2.Visible = false; label2.Visible = false; } if (comboBox1.SelectedItem.ToString() == "delete") { comboBox2.Visible = true; label2.Visible = true; textBox1.Visible = false; textBox2.Visible = false; textBox3.Visible = false; textBox4.Visible = false; label3.Visible = false; label4.Visible = false; label5.Visible = false; label6.Visible = false; //code to load combobox //code to load all id's in combobox on form load comboBox2.Items.Clear(); SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("select id from studdetails", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr[0].ToString()); } con.Close(); } if (comboBox1.SelectedItem.ToString() == "update") { comboBox2.Visible = true; label2.Visible = true; textBox2.Visible = true; textBox3.Visible = true; textBox4.Visible = true; label4.Visible = true; label5.Visible = true; label6.Visible = true; textBox1.Visible = false; label3.Visible = false; //code to load combobox //code to load all id's in combobox on form load comboBox2.Items.Clear(); SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open();

SqlCommand cmd = new SqlCommand("select id from studdetails", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr[0].ToString()); } con.Close(); } if(comboBox1.SelectedItem.ToString()=="view") { comboBox2.Visible = true; label2.Visible = true; textBox1.Visible = true; textBox2.Visible = true; textBox3.Visible = true; textBox4.Visible = true; label3.Visible = true; label4.Visible = true; label5.Visible = true; label6.Visible = true; //code to load combobox //code to load all id's in combobox on form load comboBox2.Items.Clear(); SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("select id from studdetails", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox2.Items.Add(dr[0].ToString()); } con.Close(); } } private void button1_Click(object sender, EventArgs e) { if(comboBox1.SelectedItem.ToString()=="insert") { //code to insert into record SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("insert into studdetails values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')", con); int i = cmd.ExecuteNonQuery(); if (i > 0) { MessageBox.Show("record inserted"); } con.Close(); }

if(comboBox1.SelectedItem.ToString()=="delete") { SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); //code to delete(why combox values are appended again?) SqlCommand cmd1 = new SqlCommand("delete from studdetails where id=" + comboBox2.SelectedItem.ToString(), con); int i = cmd1.ExecuteNonQuery();//which is to be used //code to refresh combobox SqlCommand cmd2 = new SqlCommand("select id from studdetails", con); SqlDataReader dr = cmd2.ExecuteReader(); //clear combox comboBox2.Items.Clear(); while (dr.Read()) { comboBox2.Items.Add(dr[0].ToString()); } con.Close(); MessageBox.Show("deleted"); } if(comboBox1.SelectedItem.ToString()=="update") { //code for updation SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("update studdetails set name='" + textBox2.Text + "',city='" + textBox3.Text + "',password='" + textBox4.Text + "' where id=" + comboBox2.SelectedItem.ToString(), con); int i = cmd.ExecuteNonQuery();//which is to be used con.Close(); MessageBox.Show("update complete"); } if(comboBox1.SelectedItem.ToString()=="view") { //code to retrieve info from studdetails SqlConnection con = new SqlConnection("data source=AMITSINGHPC\\SQLEXPRESS;database=studrecord;integrated security=yes"); con.Open(); SqlCommand cmd = new SqlCommand("select * from studdetails where id=" + comboBox2.SelectedItem.ToString(), con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { textBox1.Text = dr[0].ToString(); textBox2.Text = dr[1].ToString(); textBox3.Text = dr[2].ToString(); textBox4.Text = dr[3].ToString(); } con.Close(); } }

} }

You might also like