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 3432036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:23:59+00:00 2026-05-18T07:23:59+00:00

I have a dropdownlist which shows a list of countries from my database public

  • 0

I have a dropdownlist which shows a list of countries from my database

public void ShowCountries()
    {
        OdbcConnection conn;
        conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["jConnString"].ConnectionString);
        conn.Open();
        string sql = "SELECT iso,printable_name FROM country";

        OdbcCommand cmd = new OdbcCommand(sql, conn);


        try
        {
            //ddlCountry.DataSourceID = "country";
            ddlCountry.DataSource = cmd.ExecuteReader();
            ddlCountry.DataTextField = "printable_name";
            ddlCountry.DataValueField = "iso";
            ddlCountry.DataBind();
        }
        catch (Exception ex)
        {
            Check.Text = "3" + ex.Message;
        }
        finally
        {
            ddlCountry.Dispose();
            conn.Close();
            conn.Dispose();
        }

    }

in the aspx file this is the way how I call this databounded list

    <asp:DropDownList ID="ddlCountry" runat="server" 
DataTextField="printable_name" 
DataValueField="iso">
    </asp:DropDownList>

It shows the list but if I want to select an option other then the first one it always inserts the value of the first option en never the selected one, what am I doing wrong ?

  • 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-18T07:24:00+00:00Added an answer on May 18, 2026 at 7:24 am

    It sounds to me like the data source is being (re-)bound to the control before you are accessing the selected value, thus the selected value is always the first value in the datasource.

    Where and when does ShowCountries get called? At a guess, I’d say you’re missing a check for IsPostback in Page_Load

    if (!IsPostback) {
        // bind the datasource here, when the page initially loads
        ShowCountries();
    }
    

    Also, I don’t think you want to call Dispose() on ddlCountry in the finally block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I a have a dropdown list which is being populated from database. It is
I have a dropdownlist which lists Country names When user select any country from
I have a dropdownlist box, from which a user makes a selection. However, I
I have a custom usercontrol, which is basically a dropdownlist, prepopulated with values from
I have repeater which shows a list of users and there roles. There is
I have two dropdownlist which shows places in my asp.net MVC(C#) application. The First
i have a DropDownList which is getting data from dataset, the dropdownlist show all
I have a drop down list (ddlAccount) which i can choose an item from
I have a dropdown list (which holds numbers from 0 to 50) and html
i have dropdownlist of year which is coming dynamically.i have filled the dropdownlist using

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.