This article shows how to get data from a database using select ids from a dropdown list, where you select the ids from the dropdown list and when you click on the button you get the id's necessary information (well I include only name and city :P).
INITIAL CHAMBER
Step 1
Open Your Visual Studio 2010 and create an empty website. Provide a suitable name (gridview_demo).
Step 2
In Solution Explorer you get your empty website, then add a Web Form and SQL Server Database. By going like this.
For Web Form:
gridview_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it as -> gridviewid_demo.aspx.
For SQL Server Database:
gridview_demo (your empty website) then right-click then select Add New Item -> SQL Server database. (Add the database inside the App_Data_folder).
DATABASE CHAMBER
Step 3
In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.
Table -> tbl_data (Don't forget to make the ID as IS Identity -- True.)
Figure 1: Data Table
Make some entries in the database by going to Table -> tbl_data then right-click then Show Table Data. Don't copy my entries, make yours something else.
Figure 2: Database
DESIGN CHAMBER
Step 4
Now make some design for your application by going to gridviewid_demo.aspx and try the code like this.
Gridviewid_demo.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <style type="text/css">
- .style1
- {
- font-size: large;
- text-align: center;
- }
- .style2
- {
- width: 272px;
- }
- .style3
- {
- width: 264px;
- }
- </style>
- </head>
- <body>
- <form id="form1" runat="server">
- <div class="style1">
- <strong>Get Data From Database using Seleceted IDs</strong>
- </div>
- <table style="width:100%;">
- <tr>
- <td class="style2">
- </td>
- <td class="style3">
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td class="style2">
- </td>
- <td class="style3">
- <asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
- AutoPostBack="True" DataTextField="id" DataValueField="id" Height="16px"
- Width="118px">
- <asp:ListItem Value="0">-- Select Id--</asp:ListItem>
- </asp:DropDownList>
- </td>
- <td>
- <asp:Button ID="Button1" runat="server" BackColor="#FFFF66"
- BorderColor="#CC3300" ForeColor="#6600FF" onclick="Button1_Click"
- Text="Click Here to show the Data" />
- </td>
- </tr>
- <tr>
- <td class="style2">
- </td>
- <td class="style3">
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td class="style2">
- </td>
- <td class="style3">
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px"
- CellPadding="4" DataKeyNames="id" GridLines="Horizontal">
- <Columns>
- <asp:TemplateField HeaderText="UserId">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("id") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("id") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Name">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Bind("name") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="City">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("city") %>'>
- </asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'>
- </asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- <FooterStyle BackColor="White" ForeColor="#333333" />
- <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
- <RowStyle BackColor="White" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
- <SortedAscendingCellStyle BackColor="#F7F7F7" />
- <SortedAscendingHeaderStyle BackColor="#487575" />
- <SortedDescendingCellStyle BackColor="#E5E5E5" />
- <SortedDescendingHeaderStyle BackColor="#275353" />
- </asp:GridView>
- </td>
- <td>
- </td>
- </tr>
- <tr>
- <td class="style2">
- </td>
- <td class="style3">
- </td>
- <td>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
Your design will look like this:
Figure 3: Get Data from database
CODE CHAMBER
Step 5
Now it's time for server-side coding so that our application works. Open your gridviewid_demo.aspx.cs file and code it as in the following.
Gridviewid_demo.aspx.cs
Don't forget the namespaces as in the following:
Figure 4: Namespace
- 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;
- public partial class _Default: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
- if (!Page.IsPostBack) {
- SqlConnection con = new SqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- DropDownList1.DataSource = dt;
- DropDownList1.DataBind();
- }
- }
- protected void Button1_Click(object sender, EventArgs e) {
- SqlConnection con = new SqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data where id=" + DropDownList1.SelectedItem.Value, con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
- }
- }

Figure 5: Output
Check out the database, that who is ID=2. We will get ID=2 (purnima's data in GridView).

Figure 6: Get Data

Figure 7: Complete output
I hope you Like it, thank you for reading. Have a good day.

indradeo kumarPosted Jul 5, 2019, 2:21 AM
Nice..how to apply same as text box..
Bhavesh JadavPosted May 14, 2018, 3:57 AM
Nice sir.............
Shabab AhmadPosted Nov 15, 2017, 9:41 AM
How can i retrieve the from selected id e.g if i have two tables i.e post and comments .if i enter post id in field then it will return the data against the id and also display comment from other table.
Shalu ShaliniPosted Mar 14, 2017, 2:58 AM
I want to download content from database in (text file format part over) for particular file id, but i cant get selected rows cell value
Arul RPosted Oct 28, 2015, 5:45 AM
Nice share!!!
RakeshPosted Jun 13, 2015, 4:42 AM
Good one
Santhakumar MunuswamyPosted Jun 12, 2015, 2:40 PM
Thanks for nice article :)
Sibeesh VenuPosted Jun 12, 2015, 1:43 AM
Good One.
NitinPosted Jun 12, 2015, 1:35 AM
good one