using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; //namespace for using sqlserver components
namespace InsertApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Getgrid();
}
public void Getgrid()
{
SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS; Initial Catalog=SQLDATABASE; Integrated Security=SSPI ");
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select * from employee", con);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
con.Close();
}
// Method for Insert the values
private void btnInsert_Click(object sender, EventArgs e)
{
SqlConnection con=new SqlConnection("Data Source=SQLEXPRESS; Initial Catalog=SQLDATABASE; Integrated Security=SSPI ");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into employee values('" + txtId.Text + "','" + txtName.Text + "','" + txtDesigid.Text + "','" + txtEmploc.Text + "')", con);
int check = cmd.ExecuteNonQuery();
if (check > 0)
{
MessageBox.Show("Inserted");
}
else
{
MessageBox.Show("Not- Inserted");
}
cmd.Dispose();
con.Close();
Getgrid();
}
// Method for Update the values
private void btnUpdate_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS; Initial Catalog=SQLDATABASE; Integrated Security=SSPI ");
con.Open();
SqlCommand cmd = new SqlCommand("update employee set emp_id='" + txtId.Text + "', emp_name='" + txtName.Text + "',desig_id='" + txtDesigid.Text + "',emp_location='" + txtEmploc.Text + "' where emp_id='"+txtId.Text+"'", con);
cmd.ExecuteNonQuery();
MessageBox.Show("updated");
cmd.Dispose();
con.Close();
Getgrid();
}
// Method for Delete the values
private void btnDelete_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS; Initial Catalog=SQLDATABASE; Integrated Security=SSPI ");
con.Open();
SqlCommand cmd = new SqlCommand("delete from employee where emp_id='" + txtId.Text + "'", con);
cmd.ExecuteNonQuery();
MessageBox.Show("deleted");
cmd.Dispose();
con.Close();
Getgrid();
}
// Method for Retrieve the values
private void btnRetrieve_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS; Initial Catalog=SQLDATABASE; Integrated Security=SSPI ");
con.Open();
SqlCommand cmd = new SqlCommand("select * from employee where emp_id='" + txtId.Text + "'", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtId.Text = dr[0].ToString();
txtName.Text = dr[1].ToString();
txtDesigid.Text = dr[2].ToString();
txtEmploc.Text = dr[3].ToString();
}
}
// Method for Clear values in textbox
private void btnClear_Click(object sender, EventArgs e)
{
clearmethod(); // calling clear method
}
public void clearmethod()
{
txtId.Text = "";
txtName.Text = "";
txtDesigid.Text = "";
txtEmploc.Text = "";
}
}
}In this code snippet we will learn how to Insert, Update, Delete, and Retrieve operations in C# Windows Forms Application.
