An SqlParameter with ParameterName '@result' is not contained by this SqlParameterCollection

คำตอบ An SqlParameter with ParameterName '@result' is not contained by this SqlParameterCollection

  • 12 เมษายน 2555 16:50
     
      มีโค้ด

    Hi, when run my code, I got the error, and I checked the storeprocedure which runs good.  Please advise.

    txt = TextBox1.Text;
                this.conn.Open();
                SqlCommand cmd = new SqlCommand("VerifyEmployeeID", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("EmployeeID", SqlDbType.Int, 50).Value = txt.Trim();
                cmd.Parameters.Add("result", SqlDbType.Int, 50).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                int count = Convert.ToInt32(cmd.Parameters["@result"].Value);
                if (count > 0)
                {
                    Label1.Visible = true;
                }
                else
                {
                    Response.Write("good");
                }

    ALTER PROCEDURE [dbo].[VerifyEmployeeID]
    	
    	@EmployeeID int,
    	@result int=50 output
    AS
    BEGIN
    	if exists (select EmpID from dbMarketing.ui.tblNationalTESAdjunt
    	            where EmpID = @EmployeeID) or exists
    	            (select EmpIDGoal from dbMarketing.ui.tblNationalTESGOAL
    	           where  EmpIDGoal = @EmployeeID  )
    	       set @result = 1
    	       else
    	       set @result = 0     
    END

ตอบทั้งหมด

  • 12 เมษายน 2555 16:52
     
     คำตอบ มีโค้ด
    txt = TextBox1.Text;
                this.conn.Open();
                SqlCommand cmd = new SqlCommand("VerifyEmployeeID", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 50).Value = txt.Trim();
                cmd.Parameters.Add("@result", SqlDbType.Int, 50).Direction = ParameterDirection.Output;
                cmd.ExecuteNonQuery();
                int count = Convert.ToInt32(cmd.Parameters["@result"].Value);
                if (count > 0)
                {
                    Label1.Visible = true;
                }
                else
                {
                    Response.Write("good");
                }
    You declared your parameter as Result and then tried to access it as @Result

    Chuck

    • ทำเครื่องหมายเป็นคำตอบโดย sdnd2000 12 เมษายน 2555 17:02
    •  
  • 12 เมษายน 2555 16:53
     
     คำตอบ มีโค้ด

    hi,

    doesn't it need to be like this:

     int count = Convert.ToInt32(cmd.Parameters["result"].Value);


    Regards, Nico

    • ทำเครื่องหมายเป็นคำตอบโดย sdnd2000 12 เมษายน 2555 17:01
    •  
  • 12 เมษายน 2555 16:54
    ผู้ดูแล
     
     คำตอบ มีโค้ด

    You should be consistent. If you added parameters without @ in front, use these names. But I suggest to add them with the @ sign, e.g.

    cmd.Parameters.Add("@EmployeeID", SqlDbType.Int, 50).Value = txt.Trim();
                cmd.Parameters.Add("@result", SqlDbType.Int, 50).Direction = ParameterDirection.Output;


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • ทำเครื่องหมายเป็นคำตอบโดย sdnd2000 12 เมษายน 2555 17:01
    •  
  • 12 เมษายน 2555 17:02
     
     
    My bad. typo