First Page :
Design Page :
<div>
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("Id") %>'
CommandName="view">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Code Behind :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("select * from tblUser", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "view")
{
int Id = Convert.ToInt32(e.CommandArgument);
Session["Id"] = Id;
Response.Redirect("New.aspx");
}
}
Add another page with name as New.aspx and follow the below code...
New.aspx
Design Page :
<div>
<table class="style1">
<tr>
<td>
Id
</td>
<td style="margin-left: 40px">
<asp:TextBox ID="txtID" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
First Name
</td>
<td>
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name
</td>
<td>
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</div>
New.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int Id = Convert.ToInt32(Session["Id"]);
using (SqlConnection con = new SqlConnection("Data Source=DELL-PC;Initial Catalog=Sample;Integrated Security=True"))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from tblUser where Id=" + Id + "", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtID.Text = Convert.ToString(dr["Id"]);
txtFirstName.Text = Convert.ToString(dr["FirstName"]);
txtLastName.Text = Convert.ToString(dr["LastName"]);
}
con.Close();
}
}
}
SQL Query :
CREATE TABLE [dbo].[tblUser](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]