Bind dropdownlist in asp.net

This article shows how to bind Dropdownlist dynamically in ASP.NET web application. Follow these below steps:

Step-1: Create Table

First create a table "dbo.Tbl_Student" in database.

CREATE TABLE [dbo].[Tbl_Student] (
    [Student_Id]      INT           IDENTITY (1, 1) NOT NULL,
    [Student_Name]    VARCHAR (50)  NULL,
    [Student_Age]     INT           NULL,
    [Student_Address] VARCHAR (MAX) NULL,
    PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);

Step-2: Create a new ASP.Net web application

Now create the one sample application "Demo_Asp_DotNet" as:
  1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
  2. "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master page).
  3. Provide the web site a name such as  "Demo_Asp_DotNet" or another as you wish and specify the location.
  4. Then right-click on Solution Explorer - "Add New Item" - "bIND_dROPdOWN .aspx page".
  5. Drag and drop required controls on the <form> section of the bIND_dROPdOWN .aspx page.

Step-3: Write ConnectionString in web.config

Then Join Database with ASP.Net Webpage using ConnectionString. Write your connectionstring in web.config
<connectionStrings>
    <add name="connection" connectionString="DataSource=severname;InitialCatlog=databasename;Uid=; password=password;Integrated Security=True" />
</connectionStrings>

e.g:

<connectionStrings>
    <add name="connection" connectionString="Data Source=DBS\SQLINT;Initial Catalog=Database1;User ID=induction;Password=***********;Integrated Security=True" />
</connectionStrings>

Step-4: Design your Webform page


Write these below coding in "bIND_dROPdOWN .aspx" design page.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="DDLNAME" runat="server" Height="96px" Width="141px"></asp:DropDownList>
    </div>
    </form>
</body>
</html>

Step-5: Write Coding in Back-End / .cs page

Write these below coding in "bIND_dROPdOWN .aspx.cs" page.

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;
using System.Web.Configuration;
namespace Demo_Asp_DotNet
{
    public partial class bIND_dROPdOWN : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connection"].ToString());
        SqlDataAdapter da;
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                da = new SqlDataAdapter("select * from Tbl_Student",con);
                dt = new DataTable();
                da.Fill(dt);
                DDLNAME.DataSource = dt;
                DDLNAME.DataTextField = "Student_Name";//column name of a table, which you want to display on runtime
                DDLNAME.DataValueField = "Student_Id";
                DDLNAME.DataBind();
            }
        }
    }
}

Output:


Post a Comment

0 Comments