Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1031971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:00:51+00:00 2026-05-16T14:00:51+00:00

in my web application when i select a dropdown control which is giving error

  • 0

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
    {

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:00:51+00:00Added an answer on May 16, 2026 at 2:00 pm

    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
        {
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my web application i have 12 textboxes and 3 dropdown controls. dropdown control
I have Spatial Selection implemented on Map in my Web Application, which selects the
I am using Spring Web MVC for my application. I have 1 dropdown list
I have swing application in which there is MS excel like functionality. User can
Here's my problem: I have two select fields in my web application. The second
I have a java web application. I have an entity class which (after transformation
In the java web application need to select the file from server and print
In a .Net web application I use the public DataRow[] Select(string filterExpression) method in
I'm developing a C# web application in VS 2008. I let the user select
In VS2008, I go to File/New/Project, select ASP.NET Web Application and create a new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.