Fill/ Bind/ Load DropDown List with the data from the Sql Server table.

In this blog i am going to explain how to dynamically Bind/Load/Fill DropDownList with the data from the Sql Server table.

First of all n the design page(.aspx) place a DropDownList control as:

         <asp:DropDownList ID="ddlistQualification" runat="server" RepeatColumns="2">
        </asp:DropDownList>

Now Create a connectionstring in the web.config file under configuration tag as:
<configuration>
<connectionStrings>
                                <add name="Conn" connectionString="Data Source=.;Initial Catalog=My_DB;Integrated Security=True"/>
</connectionStrings>
</configuration>


Now, Create a Database "My_DB" in sql server and also create a table "Qualifications" having 2 fields :
         "Qualification_Id int identity(1,1) primary key not null, Qualification varchar(50)"


Now, in the code behind file(.aspx.cs) write the code as ::

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

protected void Page_Load(object sender, EventArgs e)
    {
if (!Page.IsPostBack)
        {      
            FillQualificationDropDownList();
        }
    }

private void FillQualificationDropDownList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select * from My_DB", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        adp.Fill(dt);
        ddlistQualification.DataSource = dt;
        ddlistQualification.DataTextField = "Qualification";
        ddlistQualification.DataValueField = "Qualification_Id";
        ddlistQualification.DataBind();    
    } 

Comments

Popular posts from this blog

StateCode and StatusCode Values for mostly used entities in Microsoft Dynamics CRM 2013

How to import CSV files into DataTable in C#

Search data in Gridview on Textbox Key press event using JQuery in Asp.Net- C#

go to top image