Introduction:
Here I will explain how to populate or fill or load 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 fill or populate 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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.