从数据库检索数据到下拉列表

从数据库检索数据到下拉列表

本文介绍了从数据库检索数据到下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个Web应用程序.在那儿,我有一些文本框和下拉列表的注册表格.当我从数据库中检索控件并用数据库中的数据填充控件时,在性别"下拉列表中不显示性别.始终显示第一项.如何从数据库中检索并显示在下拉列表中选择的用户的性别值.这些是我的asp和C#代码.

这是用于加载记录并将其填充到Web控件中进行编辑的代码.


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.


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...

推荐答案

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();
    }


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




or

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




谢谢
Ashish




Thanks
Ashish



这篇关于从数据库检索数据到下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:11