简体   繁体   中英

Stored procedure in C#

I am facing an issue with a stored procedures in C#, I am getting this error :

Procedure or function myQSProcedure_Delete has too many arguments specified.

I am unable to resolve this. kindly help me.

Code is as follows.

ALTER PROCEDURE [dbo].[myQSProcedure_Delete] 
@Id int
AS
BEGIN
Delete From tblfb where Id = @Id
End

My C# code is:

DataTable dt = (DataTable)ViewState["dt"];
DataTable dtDelete = (DataTable)ViewState["dtDelete"];
dtDelete.Rows.Add(dt.Rows[e.RowIndex]["Id"].ToString(),
dt.Rows[e.RowIndex]["name"].ToString(), dt.Rows[e.RowIndex]["session"].ToString(),
dt.Rows[e.RowIndex]["gender"].ToString(), dt.Rows[e.RowIndex]["email"].ToString(),
dt.Rows[e.RowIndex]["address"].ToString(), dt.Rows[e.RowIndex]["priority"].ToString(),       
dt.Rows[e.RowIndex]["comments"].ToString());
ViewState["dtDelete"] = dtDelete;
dt.Rows[e.RowIndex].Delete();
dt.AcceptChanges();
if (dt.Rows.Count < 1)
dt.Rows.Add(null, "", "", "", "", "", "", "");
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["dt"] = dt;
if (dt.Rows[0][0].ToString().Equals(""))
    {
        GridView1.Rows[0].Visible = false;
    }
SqlConnection conn = new SqlConnection("data source=Burhan\\SQLEXPRESS;database=mydb;integrated security=true");
    conn.Open();
    SqlCommand cmd = new SqlCommand("myQSProcedure_Delete", conn);
    cmd.CommandType = CommandType.StoredProcedure;
  foreach (DataRow row in dtDelete.Rows)
    {
        cmd.Parameters.Add(new SqlParameter("@Id",row[0]));
        cmd.ExecuteNonQuery();
    }
   conn.Close();
   GridView1.DataSource = dt;
    GridView1.DataBind();
    ViewState["dt"] = dt;

It looks like the command is retaining it's parameters as you loop through the rows. You should create a new command during each iteration:

foreach (DataRow row in dtDelete.Rows)
{
  using (var cmd = new SqlCommand("myQSProcedure_Delete", conn)
  {
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@Id",row[0]));
    cmd.ExecuteNonQuery();
  }
}

Your problem lies in the fact that you add a new parameter everytime you loop through. Try the below code instead.

using (SqlCommand cmd = new SqlCommand("myQSProcedure_Delete", conn))
{
    cmd.CommandType = CommandType.StoredProcedure;
    foreach (DataRow row in dtDelete.Rows)
    {
        cmd.Parameters.Clear();
        cmd.Parameters.Add(new SqlParameter("@Id",row[0]));
        cmd.ExecuteNonQuery();
    }
}

you can also add the parameter once and assign it values n times, as follows:

  using (var cmd = new SqlCommand("myQSProcedure_Delete", conn))
  {
      cmd.CommandType = CommandType.StoredProcedure;
      cmd.Parameters.Add("@Id",SqlDbType.Int);
      foreach (DataRow row in dtDelete.Rows)
      {
          cmd.Parameters["@Id"].Value = row[0];
          cmd.ExecuteNonQuery();
      }
  }

Your stored procedure only expects a single int as parameters and you are possibly sending a whole bunch.

You need to call the procedure individually for each row you want to delete and clear the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM