<asp:Button ID="savebtn" runat="server" Text="Save" OnClick="savebtn_Click" />
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand" >
<asp:LinkButton ID="editbtn" runat="server" Text="Edit" CommandName="edit" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
<asp:LinkButton ID="Deletebtn" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("ID")%>'></asp:LinkButton>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace class_01
{
public partial class student : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abc"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
Fill_grid();
}
public void Fill_grid()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from student",con);
SqlDataAdapter da =new SqlDataAdapter(cmd);
DataTable dt =new DataTable();
da.Fill(dt);
grd.DataSource=dt;
grd.DataBind();
con.Close();
}
protected void savebtn_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("insert into student(Name,Age) values('" + txtname.Text + "','" + txtage.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
Fill_grid();
}
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DHOOM3")
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from student", con);
cmd.Parameters.AddWithValue("@id", e.CommandArgument);
cmd.ExecuteNonQuery();
con.Close();
Fill_grid();
}
else if (e.CommandName == "KRISS3")
{
con.Open();
SqlCommand cmd = new SqlCommand(", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", e.CommandArgument);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
txtname.Text = dt.Rows[0][1].ToString();
txtage.Text = dt.Rows[0][3].ToString();
}
}
}
}
============================