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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:00:37+00:00 2026-06-11T15:00:37+00:00

i have a problem as subject said. i have a Table courses in sql

  • 0

i have a problem as subject said.

i have a Table courses in sql server Fields are courseID and course.

what i want is list the course in dopdown and i succeeeded but what i’m not able to do is when i select a course from dropdown list, a courseID should be selected in HiddenField/textbox/label

how to do that

here is a code i tried::

protected void Page_Load(object sender, EventArgs e)
{
    string select = "select * from courses";
    DropDownList1.Items.Add("-- Select Course --");
    DropDownList1.SelectedIndex = 0;
    DataTable dt = con.select_command(select);
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        DropDownList1.Items.Add(dt.Rows[i][1].ToString());
        DropDownList1.DataValueField = dt.Rows[i][0].ToString();
        DropDownList1.DataTextField = dt.Rows[i][1].ToString();
    }        

}

in dropdownlist i’m getting values in Page Load method

i also tried dropdownlist_selectedindex change method too to select courseID

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    LLabel1.Text = DropDownList1.SelectedValue.ToString();
}

what i’m 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-06-11T15:00:38+00:00Added an answer on June 11, 2026 at 3:00 pm

    Place a block of if (!IsPostBack) before you update the dropdown in your page_load
    The problem is that your code will re-populate the dropdown menu after the DropDownList1_SelectedIndexChanged event will be triggred.

    That will make a post request to the server and he will run the DropDownList1_SelectedIndexChanged function attached to the IndexChange event and then page_load

    because the page suppose to be loaded again
    and will result in a new selected value that will not be what you expect it to be.

    One more thing: use string identifiers when getting a value from a Row of a DataTable the code will be much more friendly that way. (Readability is very important…)

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        string select = "select * from courses";
        DropDownList1.Items.Add("-- Select Course --");
        DropDownList1.SelectedIndex = 0;
        DataTable dt = con.select_command(select);
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DropDownList1.Items.Add(dt.Rows[i]["CourseName"].ToString());
            DropDownList1.DataValueField = dt.Rows[i]["CourseID"].ToString();
            DropDownList1.DataTextField = dt.Rows[i]["CourseName"].ToString();
        }
        }        
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem finding references to this subject and would appreciate some help.
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I have one solution to the subject problem, but it’s a hack and I’m
I have a problem getting my observers working properly when on specific threads. Subject<bool>
I have a problem with stopping a service and starting it again and want
I know this subject has been discussed before but still have problem with refresh
I want to make pop3 client in PHP. I have a problem when I
I have a big problem with my SMTP server for sending emails. It's down
I have this problem. I have a graph of n nodes that I want
I have a problem for encoding my data to base64 in php. I want

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.