Hi edaadura,
Check this example. Now please 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
Default
<asp:GridView ID="GridView1" runat="server" BorderColor="White" BorderWidth="0px"
AutoGenerateColumns="false" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<h3 class="h3">
<asp:HyperLink ID="label_soalan" runat="server" Text='<%# Eval("Name")%>' NavigateUrl='<%# Eval("EmployeeID","~/Details.aspx?id={0}")%>'></asp:HyperLink></h3>
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Details
<div>
ID : <asp:Label ID="lblID" runat="server" Text=""></asp:Label>
<br />
Country : <asp:Label ID="lblCountry" runat="server" Text=""></asp:Label>
<br />
City : <asp:Label ID="lblCity" runat="server" Text=""></asp:Label>
</div>
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#
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string strcon = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,FIrstName+' '+LastName Name,Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
sda.SelectCommand = cmd;
DataTable dt = new DataTable();
sda.Fill(dt);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
}
}
}
Details
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string strcon = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
if (Request.QueryString["id"].ToString() != "")
{
using (SqlConnection con = new SqlConnection(strcon))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,Country,City FROM Employees WHERE EmployeeID=@id", con))
{
con.Open();
cmd.Parameters.AddWithValue("@id", Request.QueryString["id"].ToString());
cmd.CommandType = CommandType.Text;
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
lblID.Text = sdr["EmployeeID"].ToString();
lblCountry.Text = sdr["Country"].ToString();
lblCity.Text = sdr["City"].ToString();
con.Close();
}
}
}
}
}
VB.Net
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim strcon As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
Using con As SqlConnection = New SqlConnection(strcon)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,FIrstName+' '+LastName Name,Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter()
sda.SelectCommand = cmd
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.GridView1.DataSource = dt
Me.GridView1.DataBind()
End Using
End Using
End Using
End Sub
Details
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim strcon As String = ConfigurationManager.ConnectionStrings("ConStr").ConnectionString
If Request.QueryString("id").ToString() <> "" Then
Using con As SqlConnection = New SqlConnection(strcon)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,Country,City FROM Employees WHERE EmployeeID=@id", con)
con.Open()
cmd.Parameters.AddWithValue("@id", Request.QueryString("id").ToString())
cmd.CommandType = CommandType.Text
Dim sdr As SqlDataReader = cmd.ExecuteReader()
sdr.Read()
lblID.Text = sdr("EmployeeID").ToString()
lblCountry.Text = sdr("Country").ToString()
lblCity.Text = sdr("City").ToString()
con.Close()
End Using
End Using
End If
End If
End Sub
Screenshot
