【问题标题】:retrieving values from multiple columns in asp drop down从asp下拉列表中的多个列中检索值
【发布时间】:2016-11-09 05:32:43
【问题描述】:

我有一个下拉列表,其值是从数据库中检索的。我正在从数据库中检索 ID 和名称。

public void GetDepartment_temp()
        {
            try
            {
                DataTable dt = new DataTable();
                listBoxDepartment.ClearSelection();
                Get_Department objDAL = new Get_Department();
                dt = objDAL.Get_Hospital_Department();
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        listBoxDepartment.Items.Add(new ListItem(row["department_name"].ToString(), row["department_id"].ToString()));

                    }
                }
            }
            catch (Exception) { }
        }

我必须在文本框中显示每个部门的员工人数。假设用户选择了人事部门,那么文本框应该显示该部门的员工人数。

对于 ListBox,只能从数据库中检索两个值。在这种情况下如何显示员工人数?

public DataTable Get_Hospital_Department()
        {
            try
            {
                DataTable dt = new DataTable();
                dt = DbAccessHelper.ExecuteDataSet("p_get_hospital_department", true).Tables[0];
                return dt;
            }
            catch (Exception) { return null; }
        }

CREATE PROCEDURE [dbo].[p_get_hospital_department]
AS
BEGIN
    SET NOCOUNT ON;

    SELECT department_id
        ,department_name
    FROM [dbo].[tbl_hospital_department];
END

【问题讨论】:

  • 请分享您的代码Get_Hospital_Department()
  • 我已经提供了代码和过程。

标签: c# asp.net listbox


【解决方案1】:

For the ListBox, only two values from the database can be retrieved. 的说法不正确。您可以使用任意数量的字段填充数据表。但是,您只能像之前那样设置 Listbox 项的 Value 和 text 属性。

  1. 更改存储过程代码以获取员工人数。
  2. 将您的数据表 dt 标记为 static and public
  3. 获取数据,您可以根据需要使用数据。您可以在 listview selected index 更改的文本框中获取员工人数,如下所示:

    public static DataTable dt = new DataTable();
    
    public void GetDepartment_temp()
    {
        try
        {
    
    
            string connString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connString);
    
            SqlCommand command =
                new SqlCommand(
                    "select  Department.DepartmentID, Department.[Department Name], count( Department.DepartmentID) as empCount from Department join Employee on Department.DepartmentID = Employee.DepartmentID group by Department.DepartmentID, Department.[Department Name]",
                    connection);
    
            command.Connection.Open();
    
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);
            dt.PrimaryKey = new DataColumn[] {dt.Columns["DepartmentID"]};
            ListBox1.ClearSelection();
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow row in dt.Rows)
                {
                    ListBox1.Items.Add(new ListItem(row["Department Name"].ToString(),
                        row["DepartmentID"].ToString()));
    
                }
            }
        }
        catch (Exception ex)
        {
        }
    }
    
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
        DataRow dr = dt.Rows.Find(Convert.ToInt32(ListBox1.SelectedItem.Value));
        TextBox5.Text = dr["empCount"].ToString();
    
    }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2023-03-23
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多