【问题标题】:How to populate listbox through a parametrized stored procedure?如何通过参数化存储过程填充列表框?
【发布时间】:2012-05-01 08:24:34
【问题描述】:

我有一个 Winforms 应用程序和一个employeeListBox、departmentComboBox 和一些文本框来显示员工信息,例如fNameTextbox, lNameTextBox.....

我想通过departmentCombobox 的选定值填充employeelistBox,并从employeeListBox 填充文本框。我有这个用于选择部门员工的存储过程

ALTER PROCEDURE [dbo].[selectEmployee] 
   @departID int
-- Add the parameters for the stored procedure here
AS
   -- SET NOCOUNT ON added to prevent extra result sets from
   -- interfering with SELECT statements.
   SET NOCOUNT ON;

   -- Insert statements for procedure here
   declare @ErrorCode int



   BEGIN TRANSACTION
      if ( @ErrorCode = 0 )
      Begin
        SELECT 
            EmpID, firstname, lastName, dateOfBirth, Gender, contactNumber, maritalStatus, 
            emailAddress, resentAddress, permanentAddress, nationality, bloodGroup, 
            qualification, Skills, Experience, joiiningdate, probation, departmentID, 
            Salary, paymentMode, active 
        FROM Employee
        WHERE departmentID = @departID

set @ErrorCode = @@error
      End

      if ( @ErrorCode = 0 )
     COMMIT TRANSACTION
      else
         ROLLBACK TRANSACTION

     return @ErrorCode   

为了填充列表框,我编写了这段代码

    private void selectEmployee(int departID)
    {
        string connString = BL.dbConn.ConnStr;
        DataSet ds = new System.Data.DataSet();
        SqlConnection conn = new SqlConnection(connString);
        conn.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "dbo.selectEmployee";
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        adapter.Fill(ds);
        listBox1.DataSource = ds.Tables[0].DefaultView;
        listBox1.ValueMember = "EmpID";
        listBox1.DisplayMember = "firstname";
        cmd.Parameters.Clear();
        conn.Close();
        conn.Dispose();
    }

我不知道如何将部门ID 传递给存储过程其次如何从列表框数据集中填充文本框?

【问题讨论】:

  • 仅供参考:在您的存储过程中,“set @ErrorCode = @@error”行位于错误的位置,对您没有任何好处。你应该把它移到你的 SELECT 语句之后。
  • 编辑过的代码对你有用吗……如果你成功了,别忘了点赞并将其标记为已接受
  • +1 它没有给出该错误但没有填充列表框
  • @buddy - 你最好通过访问网站的常见问题来了解如何在 SO 上投票和接受 asnwer

标签: c# sql winforms stored-procedures


【解决方案1】:

对于传递部门ID,您需要创建sql参数并且需要使用sql命令来为您完成工作

您在代码中忘记的是da.SelectCommand = cmd;指定选择命令

        SqlConnection conn = new SqlConnection(connString);

        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
            conn.Open();
            cmd = new SqlCommand("dbo.selectEmployee", conn );
            cmd.Parameters.Add(new SqlParameter("@departID", value);
            cmd.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cmd;
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }
        catch (Exception x)
        {
            MessageBox.Show(x.GetBaseException().ToString(), "Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
        }

对于在文本框中填充值,您能否发布您想要做的代码或示例,而不是我可以进一步帮助您

【讨论】:

  • 它给了我这个错误“填充:SelectCommand.Connection 属性尚未初始化。”
  • @buddy = 我在编辑中粘贴了完整的源代码,请查看
  • @PranayRana :值的 e 在括号之外。应该是 cmd.Parameters.Add(new SqlParameter("@departID", value));
  • 它仍然没有填充列表框
  • @buddy - 设置数据源后是否调用 .DataBind()?
【解决方案2】:

您需要执行以下操作才能将 DepartmentId 添加到存储过程:

cmd.Parameters.Add(new SqlParameter("@departID", departID));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 2023-03-30
    • 2013-07-08
    • 1970-01-01
    相关资源
    最近更新 更多