【问题标题】:Csharp : Load Checked Item List based on the Value in Combo BoxCsharp:根据组合框中的值加载选中的项目列表
【发布时间】:2020-07-16 13:19:10
【问题描述】:

我无法在下面的代码中得到预期的结果。

我正在尝试从组合框中选择一项,我需要在选中列表框中获取所选项目的相应值。 示例是经理和报告人,因此如果我选择一位经理,我应该只会看到向所选经理报告的那些员工的列表。

我选中的列表框显示为空。

我在经理下面提出的代码 - 选定项目更改事件:

private void Cbreporting_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }

        try
        {
            con.Open();
            string sql1 = "Select distinct empname from tbl_emp_details where isdeleted <>1 and [reporting manager]='@lm'";
            SqlCommand com = new SqlCommand(sql1, con);
            com.Parameters.AddWithValue("@lm", Cbreporting.SelectedIndex);

            SqlCommand cmd1 = new SqlCommand(sql1, con);
            datareader = cmd1.ExecuteReader();
            while (datareader.Read())
            {
                checkedListBox1.Items.Add(datareader[0]);
            }

            con.Close();
            datareader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error", ex.ToString());
        }

    }

【问题讨论】:

  • 您面临的问题是什么?
  • 选中的列表框没有显示对应的项目。

标签: c# sql csharpcodeprovider


【解决方案1】:

我看到了 2 个命令对象 comcmd1,并且您的 SqlParameter '@lm' 已分配给 com。 Bur 你的 'ExecuteReader' 使用 cmd1 对象。修改您的代码,如下所示。

con.Open();
string sql1 = "Select distinct empname from tbl_emp_details where isdeleted <>1 and [reporting manager]='@lm'";
SqlCommand com = new SqlCommand(sql1, con);
com.Parameters.AddWithValue("@lm", Cbreporting.SelectedIndex);

datareader = com.ExecuteReader();
while (datareader.Read())
{
     checkedListBox1.Items.Add(datareader[0]);
}

con.Close();
datareader.Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2015-01-23
    • 2015-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多