Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts

I am having trouble populating the textboxes on my form. The value of the textboxes is determined by what the user the selects on the dropdownlist. When I click something from the dropdownlist nothing appears and it doesn't go into the while loop.

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace XcellIT
{


    public partial class WebForm4 : System.Web.UI.Page
    {
        //SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);
        private string CS = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
        private BusinessLogicLayer bll = null;
        USER user = null;
        CLUB club = null;


        protected void Page_Load(object sender, EventArgs e)
        {

            if (IsPostBack != true)
            {
                
                bll = new BusinessLogicLayer();

                ddlAthleteName.Items.Add(new ListItem("Select Athlete", ""));
                ddlAthleteName.AppendDataBoundItems = true;

                try
                {
                    ddlAthleteName.DataSource = bll.FillAthlete();
                    ddlAthleteName.DataTextField = "Name";
                    ddlAthleteName.DataValueField = "UserID";
                    ddlAthleteName.DataBind();
                }
                catch(Exception ex)
                {
                    throw ex;
                }
                
            }
            
            else
            {
            }            
        }
    
        protected void ddlAthleteName_SelectedIndexChanged(object sender, EventArgs e)
        {         

            string strQuery = "SELECT * FROM USER WHERE UserID = @UserID";
                    
            SqlConnection con = new SqlConnection(CS);
            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@UserID", ddlAthleteName.SelectedItem.Value);
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strQuery;
            cmd.Connection = con;

            try
            {
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                    txtGender.Text = sdr["Gender"].ToString();
                    txtEmail.Text = sdr["EmailAddress"].ToString();
                    txtPhoneNumber.Text = sdr["PhoneNumber"].ToString();
                    txtCity.Text = sdr["City"].ToString();
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
Posted
Updated 20-Sep-16 1:03am
Comments
F-ES Sitecore 20-Sep-16 6:54am    
Have you specified Autopostback="true" on your dropdown?
JustMeD 20-Sep-16 6:58am    
no
F-ES Sitecore 20-Sep-16 7:02am    
Well, there's your problem.
Karthik_Mahalingam 20-Sep-16 7:01am    
Possible solution
JustMeD 20-Sep-16 7:39am    
I inserted the AutoPostBack and the AutoPostBack works but it is still not going into the while loop. I tried a do-while loop and it didn't work as well.

1 solution

In order for the select event to fire automatically when the user changes the drop down you need to enable AutoPostback

How to use DropDownList AutoPostBack in asp.net c#[^]
 
Share this answer
 
Comments
JustMeD 20-Sep-16 8:09am    
I inserted the AutoPostBack and the AutoPostBack works but it is still not going into the while loop. I tried a do-while loop and it didn't work as well.
F-ES Sitecore 20-Sep-16 8:14am    
Learn to use the debugger to step through your code to see what is happening. Does the change event get called? What does ddlAthleteName.SelectedItem.Value return? If your event runs and the sql is executed but the while loop doesn't fire then there are no records that satisfy the query.
JustMeD 20-Sep-16 9:18am    
I fixed it, the code was correct. Apparently the word USER is a special word in SQL Server and it just needed to be like [USER]. Thank you for your time and for telling me about the AutoPostBack function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900