4
Answers

crude operations on grid view

Hi ,
Here you find my code,This is not work Properly please check and give correct code for updation and deletion.
 
 
 
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace crude
{
public partial class WebForm1 : System.Web.UI.Page
{

SqlConnection con = new SqlConnection("Data Source=ITADMIN-PC\\SQLEXPRESS;Initial Catalog=siva;User ID=sa;Password=ray1@3");
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
bind();
}

}



protected void bind()
{

con.Open();
SqlCommand cmd = new SqlCommand("select * from employee", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

GridView1.DataSource = ds;
GridView1.DataBind();

con.Close();




}



protected void Button1_Click(object sender, EventArgs e)
{

int s1 =Convert.ToInt32(TextBox1.Text);
String s2 = TextBox2.Text;

con.Open();
SqlCommand cmd = new SqlCommand("insert_emp", con);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", s1);
cmd.Parameters.AddWithValue("@NAME", s2);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);




bind();

}

protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}

protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
int s1 = Convert.ToInt32(TextBox1.Text);
String s2 = TextBox2.Text;
SqlConnection con = new SqlConnection("Data Source=ITADMIN-PC\\SQLEXPRESS;Initial Catalog=siva;User ID=sa;Password=ray1@3");
con.Open();
SqlCommand cmd = new SqlCommand("update_emp", con);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", s1);
cmd.Parameters.AddWithValue("@NAME", s2);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);

//GridView1.DataSource = ds;
//GridView1.DataBind();

//con.Close();


bind();




}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

GridView1.SelectedIndex = e.RowIndex;
int indexval = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
SqlConnection con = new SqlConnection("Data Source=ITADMIN-PC\\SQLEXPRESS;Initial Catalog=siva;User ID=sa;Password=ray1@3");
con.Open();
SqlCommand cmd = new SqlCommand("delete1_emp", con);

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ID", indexval);
bind();

}









}


 
 
 
 
 

Answers (4)