Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,I am ceating a web application.In that i have registration form with some text boxes and dropdown list.When i retrieve and fill the controls with the data from database ,In 'Gender' drop down list the actual gender is not displaying.Always the first item is being displayed.how can i retrieve from database and display a user's Gender value selected on the dropdownlist.These are my asp and C# codes.

This is the code for loading a record and filling them in web controls for editing.


C#
SqlCommand cmd = new SqlCommand("spu_RetriveUserValue", SqlConObject);
                   cmd.CommandType = CommandType.StoredProcedure;
                   DataTable dt = new DataTable();
                   cmd.Parameters.Add("@EmpID", SqlDbType.VarChar).Value = id;
                   SqlConObject.Open();
                   SqlDataAdapter da = new SqlDataAdapter(cmd);
                   da.Fill(dt);
                   if (dt.Rows.Count > 0)
                   {
                   txtEmployeeName.Text = dt.Rows[0]["Employee_Name"].ToString();
                   ddlGender.SelectedItem.Value = dt.Rows[0]["Gender"].ToString();
                   txtDateOfJoining.Text = dt.Rows[0]["JoinedDate"].ToString();
                   txtAddress.Text = dt.Rows[0]["Employee_Address"].ToString();
                   ddlCountry.SelectedItem.Text = dt.Rows[0]["Country"].ToString();
                   int CountryID =Convert.ToInt32(dt.Rows[0]["Country_ID"]);
                   Bind_ddlCity(CountryID);
                   ddlCity.SelectedItem.Text = dt.Rows[0]["City"].ToString();
                   txtEMail.Text = dt.Rows[0]["Employee_Email"].ToString();
                   ddlDeptName.Text = dt.Rows[0]["Employee_Department"].ToString();
                   }
                   else
                   {
                       lblError.Text = "No matching record found";
                   }



<pre lang="HTML">
 <asp:DropDownList ID="ddlGender" runat="server">
                               <asp:ListItem>Select</asp:ListItem>
                                    <asp:ListItem Value="M">Male</asp:ListItem>
                                    <asp:ListItem Value="F">Female</asp:ListItem>
                                </asp:DropDownList>

Help me out friends...
Posted

Try this:
C#
private void Fill_Combo()
    {
        try
        {
            combobox1.Items.Clear();
            combobox1.DataSource = //Your select query from database to retrieve data.
        }
        catch (Exception ex)
        {
            throw ex;
        }
        combobox1.DataBind();
        combobox1.Items.Insert(0, "--Select--");
        combobox1.Focus();
    }
 
Share this answer
 
Try this

C#
ddlGender.SelectedItem.Text = (dt.Rows[0]["Gender"].ToString().Trim()=="M")?"Male":"Female";

or
C#
ddlGender.SelectedValue = dt.Rows[0]["Gender"].ToString().Trim();



Thanks
Ashish
 
Share this answer
 
Hiiii

You Have to try it and i sure i will work.

after if condtion you have to be add these two line for databind.

ddlGender.DataSource=dt;
ddlGender.DataBind();


Ram Sharma
 
Share this answer
 
C#
ddlGender.SelectedValue = dt.Rows[0]["Gender"].ToString();
 
Share this answer
 
ddlGender.SelectedItem.FindByText(dt.Rows[0]["Gender"].ToString()).selected=true;
 
Share this answer
 
HI,

can you try below code

C#
ddlGender.SelectedItem.Text = dt.Rows[0]["Gender"].ToString();


or

C#
ddlGender.Selectedindex=0;



Vijay
 
Share this answer
 
v2
got it friends.. thanks for your suggestions and valuable contributions..
 
Share this answer
 
Comments
Ram Niwas Sharma 14-Aug-12 6:14am    
from which solution?
thanks frenz. got it now...it s working fine..
 
Share this answer
 

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