The function or procedure has too many arguments

0

Help me with this error occurs if I execute a stored procedure more than once in the following process: the error occurs inside the foreach when it enters a second time

public string habilitarcredencial(int idusuariom, GridView sistemas, int reintentar)

{

string strsql;

strsql = "HabilitarCredencial";

CadenaConexion cc = new CadenaConexion();
SqlConnection cnn = new SqlConnection(cc.ConnectionString());

SqlCommand da = new SqlCommand();
da.Connection = cnn;
cnn.Open();

da.CommandType = CommandType.StoredProcedure;

string error = string.Empty;
string erroracumulador = string.Empty;

foreach (GridViewRow item in sistemas.Rows)

{

 TextBox boton = (TextBox)item.FindControl("FechaI");
 TextBox boto2 = (TextBox)item.FindControl("FechaF");

 if (boton.Text!="" && boto2.Text!="")
 {

 da.Parameters.Add("@idusuario", SqlDbType.Int).Value = idusuariom;
 da.Parameters.Add("@idsistema", SqlDbType.Int).Value = 
 item.Cells[2].Text.ToString();
 da.Parameters.Add("@fechai", SqlDbType.DateTime).Value = boton.Text;
 da.Parameters.Add("@fechan", SqlDbType.DateTime).Value = boto2.Text;
 da.Parameters.Add("@reintentar", SqlDbType.Bit).Value = reintentar;

 da.Parameters.Add("@error", SqlDbType.VarChar, 8000).Direction = 
 ParameterDirection.Output;

 da.CommandText = strsql;
 da.ExecuteNonQuery();
 erroracumulador = Convert.ToString(da.Parameters["@error"].Value);
 error = " "+error+" "+ erroracumulador +" ";
 } 

}

cnn.Close();

return error;
}
    
asked by Agustin Coronel 30.04.2018 в 05:59
source

1 answer

0

You need to clean the parameters every time you iterate through the cycle, in the current way you run it for each iteration of foreach you add parameters without cleaning the previous ones so just add the following line to the code

da.Parameters.Clear();

The cycle is as follows:

foreach (GridViewRow item in sistemas.Rows)

{

 TextBox boton = (TextBox)item.FindControl("FechaI");
 TextBox boto2 = (TextBox)item.FindControl("FechaF");

 if (boton.Text!="" && boto2.Text!="")
 {

 da.Parameters.Add("@idusuario", SqlDbType.Int).Value = idusuariom;
 da.Parameters.Add("@idsistema", SqlDbType.Int).Value = 
 item.Cells[2].Text.ToString();
 da.Parameters.Add("@fechai", SqlDbType.DateTime).Value = boton.Text;
 da.Parameters.Add("@fechan", SqlDbType.DateTime).Value = boto2.Text;
 da.Parameters.Add("@reintentar", SqlDbType.Bit).Value = reintentar;

 da.Parameters.Add("@error", SqlDbType.VarChar, 8000).Direction = 
 ParameterDirection.Output;

 da.CommandText = strsql;
 da.ExecuteNonQuery();
 erroracumulador = Convert.ToString(da.Parameters["@error"].Value);
 error = " "+error+" "+ erroracumulador +" ";
 da.Parameters.Clear(); //Limpiamos los parametros
 } 

}

Greetings

    
answered by 30.04.2018 в 15:26