Here I will explain how to bind or show data in dropdownlist from database in asp.net using C#.net and VB.NET.
Column Name
|
Data Type
|
Allow Nulls
|
UserId
|
Int
(set Identity=true)
|
No
|
UserName
|
varchar(50)
|
Yes
|
Location
|
Varchar(50)
|
Yes
|
<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>
|
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
|
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();
}
}
|
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
|
|
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 Email
|
|||
|
|

Subscribe by RSS
Subscribe by Email
10 comments :
hai
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
Thanks Suresh.
thanks....
simply superb
Nice One
perfecto, lo mas sencillo que vi en la red, gracias
Ty!!!
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.
Thanks Friend