Hi indradeo,
Check this sample. now take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
Page1.aspx
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Customer ID" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDetail" Text="Get Details" runat="server" OnClick="OnGetDetails" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Details.aspx
<table>
<tr>
<td colspan="2" align="center">Customer Details</td>
</tr>
<tr>
<td>Customer ID</td>
<td><asp:TextBox ID="txtID" runat="server" /></td>
</tr>
<tr>
<td>Name</td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
</tr>
<tr>
<td>City</td>
<td><asp:TextBox ID="txtCity" runat="server" /></td>
</tr>
<tr>
<td>Country</td>
<td><asp:TextBox ID="txtCountry" runat="server" /></td>
</tr>
</table>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
Page1.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindCustomers();
}
}
protected void OnGetDetails(object sender, EventArgs e)
{
GridViewRow row = ((sender as Button).NamingContainer as GridViewRow);
string customerID = row.Cells[0].Text;
string name = row.Cells[1].Text;
string city = row.Cells[2].Text;
string country = row.Cells[3].Text;
Response.Redirect("Details.aspx?CustomerID=" + customerID + "&Name=" + name + "&City=" + city + "&Country=" + country);
}
private void BindCustomers()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
}
}
Details.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
txtID.Text = Request.QueryString["CustomerID"];
txtName.Text = Request.QueryString["Name"];
txtCity.Text = Request.QueryString["City"];
txtCountry.Text = Request.QueryString["Country"];
}
}
VB.Net
Page1.asx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindCustomers()
End If
End Sub
Protected Sub OnGetDetails(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = (TryCast((TryCast(sender, Button)).NamingContainer, GridViewRow))
Dim customerID As String = row.Cells(0).Text
Dim name As String = row.Cells(1).Text
Dim city As String = row.Cells(2).Text
Dim country As String = row.Cells(3).Text
Response.Redirect("Details.aspx?CustomerID=" & customerID & "&Name=" & name & "&City=" & city & "&Country=" & country)
End Sub
Private Sub BindCustomers()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 CustomerID,ContactName,City,Country FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Details.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
txtID.Text = Request.QueryString("CustomerID")
txtName.Text = Request.QueryString("Name")
txtCity.Text = Request.QueryString("City")
txtCountry.Text = Request.QueryString("Country")
End If
End Sub
Screenshot