Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Show Bind Data to Asp.net Dropdownlist from Database in C#, VB.NET

Oct 9, 2012
Introduction:

Here I will explain how to bind or show data in dropdownlist from database in asp.net using C#.net and VB.NET.

Description:

In previous posts I explained many articles relating asp.net, jQuery, SQL Server, JavaScript. Now I will explain how to bind or show data in dropdownlist from database in asp.net using C#.net and VB.NET.

Before implement this example first design one table UserInformation in your database as shown below

Column Name
Data Type
Allow Nulls
UserId
Int (set Identity=true)
No
UserName
varchar(50)
Yes
Location
Varchar(50)
Yes
Once table designed in database write the following code in your aspx page


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>how to show data in dropdownlist from database in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<b>Selected UserName:</b>
<asp:DropDownList ID="ddlCountry" runat="server" />
</div>
</form>
</body>
</html>
Now add the following namespaces in code behind

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
After add namespaces write the following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindContrydropdown();
}
}
/// <summary>
/// Bind COuntrydropdown
/// </summary>
protected void BindContrydropdown()
{
//conenction path for database
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select UserId,UserName FROM UserInformation", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ddlCountry.DataSource = ds;
ddlCountry.DataTextField = "UserName";
ddlCountry.DataValueField = "UserId";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem("--Select--", "0"));
con.Close();
}
}
VB.NET Code


Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Partial Class VBSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindContrydropdown()
End If
End Sub
''' <summary>
''' Bind COuntrydropdown
''' </summary>
Protected Sub BindContrydropdown()
'conenction path for database
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select UserId,UserName FROM UserInformation", con)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
ddlCountry.DataSource = ds
ddlCountry.DataTextField = "UserName"
ddlCountry.DataValueField = "UserId"
ddlCountry.DataBind()
ddlCountry.Items.Insert(0, New ListItem("--Select--", "0"))
con.Close()
End Using
End Sub
End Class
Demo

If you want to learn more dropdownlist articles check this


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

19 comments :

surendrakumar nimmagadda said...

hai

Vetri said...

Hi...
I am your fan. I used many of your coding samples in my projects.

But In recent days your articles are very simple and basic things.

Could you please started to post on advanced things like MVC4 articles....

thanks
Vetri

German Alex said...

Thanks Suresh.

K.Ramalingam said...

thanks....

Gobi Krishnan said...

simply superb

ప్రసాద్ విశ్వనాధ said...

Nice One

laucaroso said...

perfecto, lo mas sencillo que vi en la red, gracias

Anonymous said...

Ty!!!

Atul Enjoying with studies said...

Hi suresh i want to display date in dropdown one ddlfor year in which year should be display likewise month and date.
ex:20130501 data in my data base now in page load 2013 in ddlyear and june in dllmonth should be display.
how will i do this.

Anonymous said...

Thanks Friend

Pratibha Pandey said...

thanks

Zimasa Yokwana said...

Very helpful, thanks.

ahmed khiran said...

thanks , but i don't repeat the same item in droplistdown ,

such as the droplistdown contain cc ,cc ,cc ,cc , & i hope to see 1 cc not 3

how ?

thanks

सोमनाथ चतुर्वेदी said...

Dhanyvaad Guruji

manvir singh said...

hello sir

how i bind drop down list with detail view .....

Mukesh Dutt said...
This comment has been removed by the author.
Mukesh Dutt said...

Thanks solution for add new item for drop list at zero index by the new keyword.

Anonymous said...

how to load country, state, city dropdown list on a gridview's inserting and updating mode using three tier architecture.
I want its solution immediately.

Ritesh Singh said...

Thanks solution for add new item for drop list

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.