Geeks With Blogs


Microsoft MVP


Moderator at CodeASP.NET

Quiz Master

free counters
Free counters
Added on January 19,2012

Follow Me @vmsdurano

A bit About Me

The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.

Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write a simple demo about this because I always encounter this issue at the forums. Basically, this demo shows how to pre select multiple list items in the ListBox control based from database value.

Assuming that we have this table definition with data below:

As you can see, the Table has a "IsSelected" boolean field which indicates that a particular employee name is selected or not. Note that in this demo, I named the Table above as "Table1".

Now lets set up our ListBox at the ASPX source. Assuming that we have this mark up below:

 <asp:ListBox ID="ListBox1" runat="server" Height="149px"
              SelectionMode="Multiple" Width="113px">


Note: Since we need to pre-select multiple items in the ListBox then be sure to set the SelectionMode property of the ListBox to Multiple.

Here are the code blocks for pre-selecting the ListBox items based from the database data:

private DataTable GetData(){

        DataTable dt = new DataTable();
        SqlConnection conn = new SqlConnection(GetConnectionString());
        string sqlStatement = "SELECT * FROM Table1";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        catch (System.Data.SqlClient.SqlException ex)
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        return dt;

protected void Page_Load(object sender, EventArgs e){
        if (!Page.IsPostBack)
            DataTable dt = GetData();
            if (dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    bool isSelected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]);
                    if (isSelected)
                        ListBox1.Items[i].Selected = true;

As you can see the code above was pretty self explanatory and straight forwad.

You can see the page output below:

That's it! I hope you will find this example useful!

Technorati Tags: ,,

Posted on Monday, June 1, 2009 1:45 PM ADO.NET , ASP.NET , C# | Back to top

Comments on this post: Pre-Select Multiple ListBox items based on Database Value

# re: Pre-Select Multiple ListBox items based on Database Value
Requesting Gravatar...
For single Pre-Select Multiple ListBox This is perfect but for
But Multiple text boxes Ex.Multiple Country,Multiple State,Multiple City and all are dependable on each other means
Country->State->City.So Can u sugest any solution or code or project
Left by Nandakishor on Feb 12, 2013 6:00 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: