Set DropDownList Item selected Based on DataReader object column value in ASP.Net using C#

Last Reply on Mar 16, 2018 03:23 AM By mukesh1

Posted on Mar 16, 2018 03:19 AM

I have a asp.net web form which has textboxes and dropdown lists. I am populating the form from the database based on the primary key. The selected items of the dropdownlist are coming at the first position apart from its originial index. how to solve it?

  protected void Button3_Click(object sender, EventArgs e)
    {
    
        con.Open();
        string str;
        str = "select * from (((service_master inner join serv_grp_mas on service_master.serv_grp_cd=serv_grp_mas.serv_grp_cd)inner join base_uom_mas on service_master.base_uom_cd= base_uom_mas.base_uom_cd)inner join tax_ind_mas on service_master.tax_ind=tax_ind_mas.tax_ind) where req_no='" + TextBox1.Text.Trim() + "' ";
        OleDbCommand com = new OleDbCommand(str, con);
        OleDbDataReader dr = com.ExecuteReader();
        dr.Read();
          if (dr.HasRows==true)
          {
              TextBox9.Text = dr["req_dt"].ToString();
               Session["UserName"]=dr["req_by"].ToString();
              TextBox3.Text= dr["ser_cd"].ToString();
              TextBox4.Text=dr["Serv_desc"].ToString(); 
              DropDownList3.SelectedItem.Text = dr["serv_grp_desc"].ToString();
              DropDownList1.SelectedItem.Text = dr["base_uom_desc"].ToString();
              TextBox5.Text=dr["sac_cd"].ToString();
              TextBox6.Text=dr["ser_long_desc"].ToString();
              DropDownList2.SelectedItem.Text = dr["tax_ind_desc"].ToString();
              TextBox7.Text=dr["active_ind"].ToString();
              TextBox8.Text=dr["del_ind"].ToString();
          }
          else
          {
              WebMsgBox.Show("The entry does not exist");
              //TextBox1.Text = "";
              TextBox9.Text = "";
              TextBox2.Text = "";
              TextBox3.Text = "";
              TextBox4.Text = "";
              DropDownList3.SelectedItem.Text = "";
              DropDownList1.SelectedItem.Text = "";
              TextBox5.Text = "";
              TextBox6.Text = "";
              DropDownList2.SelectedItem.Text = "";
              TextBox7.Text = "";
              TextBox8.Text = "";

          }
              dr.Close();
              con.Close();
          }

 

Posted on Mar 16, 2018 03:22 AM

Try to find the Item Text or Item Value and then set the selected true if it exists.

// If base_uom_desc based on dropdown item Text ten do below line of code 
if (DropDownList1.Items.FindByText(dr["serv_grp_desc"].ToString()) != null)
{
    DropDownList1.Items.FindByText(dr["serv_grp_desc"].ToString()).Selected = true;
}
 
// If base_uom_desc based on dropdown item value ten do below line of code 
if (DropDownList1.Items.FindByValue(dr["serv_grp_desc"].ToString()) != null)
{
    DropDownList1.Items.FindByValue(dr["serv_grp_desc"].ToString()).Selected = true;
}

 


Posted on Mar 16, 2018 03:23 AM

Coz u define that 

 

DropDownList3.SelectedItem.Text = dr["serv_grp_desc"].ToString();
            DropDownList1.SelectedItem.Text = dr["base_uom_desc"].ToString();
          

That's y selected item at first position in dropdownlist.