0
votes

Asp.net code :

     Select Employee : </b>
    <asp:DropDownList ID="DropDownListSelectEmployee" runat="server" AutoPostBack="true" 
OnSelectedIndexChanged="DropDownListSelectEmployee_SelectedIndexChanged"
 OnTextChanged="DropDownListSelectEmployee_TextChanged"  >
                                    </asp:DropDownList>

Code behind :

  public void DropDownListSelectEmployee_Fill()
        {
            DropDownListSelectEmployee.Items.Clear();
            string q = "select username from nworksuser where _type='Employee' or _type='Admin_Employee';";
            MySqlCommand cmd = new MySqlCommand(q, conn);
            conn.Open();
            string user = "";
            MySqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                user = rdr.GetString("username");
                DropDownListSelectEmployee.Items.Add(user);
            }
            conn.Close();
        }
 protected void DropDownListSelectEmployee_TextChanged(object sender, EventArgs e)
        {
            string q = "select uid,fname,mname,lname,date_format(hire_date,'%Y-%m-%d'),desg,date_format(dob,'%Y-%m-%d'),AddressLine1,AddressLine2,state,city,pincode,country,MobileNo,UserActive,username,date_format(annivarsary,'%Y-%m-%d'),email from nWorksUser where Username='" + DropDownListSelectEmployee.Text + "'";
            MySqlCommand cmd = new MySqlCommand(q, conn);
            conn.Open();
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(q, conn);
            DataTable dt = new DataTable();
            dataAdapter.Fill(dt);
            foreach (DataRow dr in dt.Rows)
            {

                txtFirstName.Text = dr[1].ToString();
                txtMiddleName.Text = dr[2].ToString();
                txtLastName.Text = dr[3].ToString();
                txtMobile.Text = dr[13].ToString();
                txtUsername.Text = dr[15].ToString();
                txtState.Text = dr[9].ToString();
                txtPincode.Text = dr[11].ToString();
                txtEmail.Text = dr[17].ToString();
                txtDob.Text = dr[6].ToString();
                txtDesignation.Text = dr[5].ToString();
                txtDateofHire.Text = dr[4].ToString();
                txtDateofAnnivarsary.Text = dr[16].ToString();
                txtCountry.Text = dr[12].ToString();
                txtCity.Text = dr[10].ToString();
                txtAddressLine2.Text = dr[7].ToString();
                txtAddressLine1.Text = dr[8].ToString();

                if (dr[14].ToString().Length == 4)
                {
                    RdoGender.SelectedIndex = 1;
                }
                else
                {
                    RdoGender.SelectedIndex = 2;
                }
                conn.Close();
            }

        }
 protected void DropDownListSelectEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

Drop down list contains users name from database. Now I want that, as you select any user from drop down list it should show his details in form and its working. But problem is that after choosing any one user, that name appears in drop down for moment and reset it to the first name of drop down list any showing details of its related instead of selected. Its not accepting any other name except first name drop down list. I am not getting where is the problem. Please help me.

1

1 Answers

0
votes

You need to wrap the code in DropDownListSelectEmployee_Fill with if (!Page.IsPostBack). On a post back the drop down contents will be repopulating, causing its state to reset, which is why you're seeing the "unexpected behaviour".

Checking for post backs, you can make certain the drop down isn't affected on a selection change:

public void DropDownListSelectEmployee_Fill()
{
    if (!Page.IsPostBack) 
    {
        DropDownListSelectEmployee.Items.Clear();
        string q = "select username from nworksuser where _type='Employee' or _type='Admin_Employee';";
        MySqlCommand cmd = new MySqlCommand(q, conn);
        conn.Open();
        string user = "";
        MySqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
        {
            user = rdr.GetString("username");
            DropDownListSelectEmployee.Items.Add(user);
        }
        conn.Close();
    }
}