C#.Net DataGridView Insert,Update,Select,Delete using Stored procedure 1.create new winform add one datagrid ,3 label and 3 TextBox
2. add 3 button for insert,update,delete
3.create stored procedure for all
Inset query using stored Procedure:
create procedure insertemp1 (@id int,@name varchar(100),@sal int)
As
insert into parthi values(@id,@name,@sal)
Select query using stored Procedure:
Create Procedure insertemp2
As
SELECT * FROM parthi
Update query using stored procedure:
create procedure insertemp3 (@id int,@name varchar(100),@sal int)
As
update parthi set name=@name,salary=@sal where id=(@id)
Delete query using stored procedure:
create procedure insertemp4(@id int) as
delete from parthi where id=@id
Form1.cs:
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 gridEdit
{
public partial class Form1 : Form
{
SqlConnection con =new SqlConnection("server =192.168.3.150;uid=sa;pwd=sa@1234;database=nss1");
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//this.Size = Screen.PrimaryScreen.Bounds.Size;
//this.Location = new Point(0, 0);
SqlCommand command = new SqlCommand("insertemp2", con);
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds, "parthi");
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "parthi";
}
private void btninsert_Click(object sender, EventArgs e)
{
int txtid =Convert.ToInt32(textBox1.Text);
string txtname = textBox2.Text;
int txtsal = int.Parse(textBox3.Text);
SqlCommand cmd = new SqlCommand("insertemp1", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertemp1";
cmd.Parameters.AddWithValue("@id", txtid);
cmd.Parameters.AddWithValue("@name", txtname);
cmd.Parameters.AddWithValue("@sal", txtsal);
cmd.ExecuteNonQuery();
MessageBox.Show("inserted......");
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
int txtid = Convert.ToInt32(textBox1.Text);
string txtname = textBox2.Text;
int txtsal = int.Parse(textBox3.Text);
SqlCommand cmd = new SqlCommand("insertemp3", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertemp3";
cmd.Parameters.AddWithValue("@id", txtid);
cmd.Parameters.AddWithValue("@name", txtname);
cmd.Parameters.AddWithValue("@sal", txtsal);
cmd.ExecuteNonQuery();
MessageBox.Show("updated......");
con.Close();
}
private void button3_Click(object sender, EventArgs e)
{
int txtid = Convert.ToInt32(textBox1.Text);
SqlCommand cmd = new SqlCommand("insertemp4", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "insertemp4";
cmd.Parameters.AddWithValue("@id", txtid);
cmd.ExecuteNonQuery();
MessageBox.Show("deleted......");
con.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dataGridView1.SelectedCells[0].RowIndex;
textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
}
}
}
final output window: