tags:

views:

24

answers:

1

hi,

in my web application when i select a dropdown control which is giving error like. Cannot have multiple items selected in a DropDownList. This is my code...

    try
    {             
        ddlState.Items.Clear();
        ddlState.Enabled = true;
        ListItem li1 = new ListItem();
        li1.Value = "0";
        li1.Text = "Select State";
        ddlState.Items.Add(li1);
        clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
        DataSet ds = clsStates.selectStateBl();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            li1 = new ListItem();
            li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
            li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();
            ddlState.DataTextField = "statename";
            ddlState.DataValueField = "stateid";
            ddlState.Items.Add(li1);
        }
    }
    catch
    {

    }

this is binding the countries code...

     try
    {

        ListItem li = new ListItem();

        DataSet ds = clsCountrys.selectCountryB();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            li = new ListItem();
            li.Text = ds.Tables[0].Rows[i]["countryname"].ToString();
            li.Value = ds.Tables[0].Rows[i]["countryid"].ToString();
            if (li.Value == Session["usercountry"].ToString())
                li.Selected = true;
            ddlCountry.DataTextField = "countryname";
            ddlCountry.DataValueField = "countryid";
            ddlCountry.Items.Add(li);
        }
    }
    catch
    {

    }

and this is state binding to dropdown control..

     try
    {
        ddlState.Items.Clear();
        ddlState.Enabled = true;
        ddlState.ClearSelection();
        ListItem li1 = new ListItem();
        li1.Value = "0";
        li1.Text = "Select State";
        ddlState.Items.Add(li1);
        clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
        DataSet ds = clsStates.selectStateBl();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            li1 = new ListItem();
            li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
            li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();
            if (Session["userstate"].ToString() == li1.Value)
                li1.Selected = true;
            ddlState.DataTextField = "statename";
            ddlState.DataValueField = "stateid";
            ddlState.Items.Add(li1);
        }
    }
    catch
    {

    }
A: 

Make use of ddlState.ClearSelection() method to clear default selection.

Check this post : ASP.NET Tips: DropDownList.ClearSelection() to avoid "Cannot have multiple items selected in DropDownList"

EDIT

  try
    {
        ddlState.ClearSelection();
        ddlState.Items.Clear();
        ddlState.Enabled = true;

        ListItem li1 = new ListItem();
        li1.Value = "0";
        li1.Text = "Select State";
        ddlState.Items.Add(li1);
        clsStates.Countryid = int.Parse(ddlCountry.SelectedValue.ToString());
        DataSet ds = clsStates.selectStateBl();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            li1 = new ListItem();
            li1.Text = ds.Tables[0].Rows[i]["statename"].ToString();
            li1.Value = ds.Tables[0].Rows[i]["stateid"].ToString();

            ddlState.DataTextField = "statename";
            ddlState.DataValueField = "stateid";
            ddlState.Items.Add(li1);

        }

          //code updated by pranay rana
           ddlState.ClearSelection();
              ListItem li = ddlState.Items.FindByValue(Session["userstate"].ToString());
                if (li != null)
                {
                    li.Selected = true;
                }
           //code updated by pranay rana
    }
    catch
    {

    }
Pranay Rana
@pranay Rana i write u r code in the event but it is not working
Surya sasidhar
@Surya sasidhar : - just write this in the function where are you binding value to dropdownlist i.e after binding data
Pranay Rana
@prany Rana actually i have 2 drop down button one for binding the countryies and when i select country in one dropdown i am binding the state of that country i have two dropdown controls.
Surya sasidhar
just writ above code when you bind the values for the both of dropdown box will work for you
Pranay Rana
Check the posted link will help you
Pranay Rana
ya i check that one i just edited my question plese check out once thank you for reply
Surya sasidhar
@pranay Rana no yaar it is not working thank you for reply
Surya sasidhar
@Surya sasidhar :- just try to put this line ddlState.ClearSelection(); before you selecting value programmaticaly
Pranay Rana
Pranay rana can u tell me where exactly in event or in binding the data
Surya sasidhar
@Surya sasidhar : just copy and paste code given by me and see its working or not i.e edit section code to bind state dropdown box
Pranay Rana
ya thank you Pranay it is working fine
Surya sasidhar