Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CRUD Operations-

Establishing Connection Form Load-


private void Form1_Load(object sender, EventArgs e)
{
radioButton1.Checked = true;
button3.Enabled = true;
button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
try
{
string Source = "server=.;Initial Catalog=employee;Integrated Security=True";
conn = new SqlConnection(Source);
conn.Open();
}
catch (Exception x){
MessageBox.Show(x+"");
}

Search Record as Per ID-


private void button4_Click(object sender, EventArgs e)
{
try
{
string query = "select * from emp where empid='" + textBox1.Text + "'";

SqlCommand cmd = new SqlCommand(query, conn);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

sda.Fill(dt);

dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex+"");
}

}
Insert Record-
private void button1_Click(object sender, EventArgs e)
{
try
{
string query = "insert into emp values('" + textBox1.Text + "','" + textBox2.Text + "','"+textBox3.Text+"')";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Employee Record Inserted ");

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

MessageBox.Show(x+"Please Fill The Form With Valid Details");

}
}

Delete Record-
private void button2_Click(object sender, EventArgs e)
{
try
{
string query = "delete from emp where empid='"+textBox1.Text+"'";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Employee With id "+textBox1.Text+" deleted");

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

MessageBox.Show(x + "Please Fill The Form With Valid Details");

}
}
Update Record-
private void button3_Click(object sender, EventArgs e)
{
try
{
string query = "update emp set empname='"+textBox2.Text+"',age='"+textBox3.Text+"' where
empid='"+textBox1.Text+"'";

SqlCommand cmd = new SqlCommand(query, conn);

cmd.ExecuteNonQuery();

MessageBox.Show("Record Updated With id "+textBox1.Text);

textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}

catch (Exception x)
{

MessageBox.Show(x + "Please Fill The Form With Valid Details");

}
}
Insert/Update Radio Button-
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox2.Enabled = true;
textBox3.Enabled = true;
button3.Enabled = true;
button4.Enabled = false;
button1.Enabled = true;
button2.Enabled = false
}

Search/Delete Radio Button


private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked)
MessageBox.Show("Enter id For Search and Delete");
textBox2.Enabled = false;
textBox3.Enabled = false;
button3.Enabled = false;
button4.Enabled = true;
button1.Enabled = false;
button2.Enabled = true;
}

Table-

You might also like