Fill Dropdown List using Database in ASP.NET using C#

Fill Dropdown List using Database in ASP.NET using C#


Posted in : ASP.Net Posted on : December 9, 2010 at 5:58 PM Comments : [ 0 ]

In this article we will introduce How can we fill Dropdown List using Database in ASP.NET using C#

Fill Dropdown List using Database in ASP.NET using C#

In this example we use a Dropdown List and a Grid View. The Dropdown List fills from column value of the table. If we select an item from Dropdown List the record of selected field will be shown in Grid View.

DropDownListDataBase.aspx (Design Page):

DropDownListDataBase.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="DropDownListDataBase.aspx.cs" 
Inherits="DropDownListDataBase" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">Fill DropDownList using DataBase</h2>
<br />
<strong>Select Name:</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" CellPadding="4" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</asp:Content>

DropDownListDataBase.aspx.cs (C# code file):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Configuration;
public partial class DropDownListDataBase : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
private string s;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList1.Items.Insert(0, new ListItem("---Select---", "---Select---"));
FillDropDownList();
}
}
// Show data in GridView
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
cmd = new SqlCommand("Select * from Student where StudentName='" + DropDownList1.SelectedItem.ToString() + "'", con);
dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
dr.Close();
con.Close();
}
// Fill Dropdownlist
public void FillDropDownList()
{
s = WebConfigurationManager.ConnectionStrings["ChartDatabaseConnectionString"].ConnectionString;
con = new SqlConnection(s);
con.Open();
cmd = new SqlCommand("Select StudentName from Student", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(dr[0].ToString());
}
dr.Close();
con.Close();
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics