Wednesday 5 September 2012

Beginning .Net : Populating or Fill dropdownlist from Database table with C# Examples and VB.Net Examples

There are a situations where you want to fill dropdownlist or combobox or listbox from database table.
For that you need to get database table records in to DataSet or DataTable and after that you fill dropdownlist from DataTable.
For that we need to set Three properties of dropdownlist control.
Properties are:
  • DataSource : Assign Datable object
  • DataTextField : This is column name of database table which you want to display in dropdownlist box.
  • DataValueField : This is column name of database table which use to identify which item is selected in list.
 After assigning this properties You need to call DataBind() Method of drop down list.

You can also say that fill dropdownlist using ADO.Net.

Here are sample example for this.
In this example we retrieve "product_master" table data from database and populate in to drop down list.

C# Examples :
        SqlConnection objConn = new SqlConnection();
        SqlDataAdapter objADP = new SqlDataAdapter();
        DataSet oDs = new DataSet();
        objConn.ConnectionString = @"Data Source=.\SQLEXPRESS;" +
                                   "Initial Catalog=TempDatabase;" +
                                   "User ID=sa;Password=sa;";

        SqlCommand objcmd = new SqlCommand();
        objcmd.CommandText = "Select product_id , product_name from product_master";
        objcmd.CommandType = CommandType.Text;
        objConn.Open();
        objcmd.Connection = objConn;
        

        objADP.SelectCommand = objcmd;
        objADP.Fill(oDs,"tblProductMaster");

        objConn.Close();

        ddlProductList.DataSource = oDs.Tables["tblProductMaster"];
        ddlProductList.DataTextField = "product_name";
        ddlProductList.DataValueField = "product_id";
        ddlProductList.DataBind();

        objADP = null;

VB.net Examples :

        Dim objConn As New SqlConnection()
        Dim objADP As New SqlDataAdapter()
        Dim oDs As New DataSet()
        objConn.ConnectionString = "Data Source=.\SQLEXPRESS;" & _
                                   "Initial Catalog=TempDatabase;" & _
                                   "User ID=sa;Password=sa;"

        Dim objcmd As New SqlCommand()
        objcmd.CommandText = "Select product_id , product_name from product_master"
        objcmd.CommandType = CommandType.Text
        objConn.Open()
        objcmd.Connection = objConn


        objADP.SelectCommand = objcmd
        objADP.Fill(oDs, "tblProductMaster")

        objConn.Close()

        ddlProductList.DataSource = oDs.Tables("tblProductMaster")
        ddlProductList.DataTextField = "product_name"
        ddlProductList.DataValueField = "product_id"
        ddlProductList.DataBind()

        objADP = Nothing

Output : 


You can also fill listbox with same way.


For Beginning .Net articles. Click Here...

Click Here to View "Fill dropdownlist from Enum with C# Examples and VB.Net Examples". Click Here

This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


No comments:

Post a Comment