Hi @venu,
Html Code..
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="City Id" DataField="CityId" />
<asp:TemplateField HeaderText="City Name">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("CityName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT CityId, CityName FROM City");
GridView1.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}
Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
Here in Html Code Section - I have a taken a gridView to display the result set.
Lets focus to your query.
Get data function this function will collect the result set from database as per the query you have provided to this method.
this method keeps the result set in datatable.
On page Load event this datatable is directly bound to Gridview.
This method is made to create an interaction with database as you request by using query.
GetData it used to display the data.
For furthur information go to this article link and read carefully.
http://www.aspsnippets.com/Articles/How-to-populate-DropDownList-in-GridView-in-ASPNet.aspx