Monday, March 19, 2012

Another SqlParameterCollection include ParameterName“@Id” SqlParameter。

error:
Another SqlParameterCollection include ParameterName"@.Id" SqlParameter?

if I remove one of " RunProc("test",param);" ,there will be right

private void Page_Load(object sender, System.EventArgs e)
{
SqlParameter param = new SqlParameter("@.Id", SqlDbType.Int);
param.Value = 1;

RunProc("test",param);
RunProc("test",param); //if i remove this line ,it's OK
}
public void RunProc(string procName, SqlParameter pram)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
con.Open();

SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(pram);
cmd.ExecuteNonQuery();

con.Close();
}You can't use the same SqlParameter multiple times. Try something like this:

private void Page_Load(object sender, System.EventArgs e)
{
SqlParameter param = new SqlParameter("@.Id", SqlDbType.Int);
param.Value = 1;
RunProc("test",param);

SqlParameter param2 = new SqlParameter("@.Id", SqlDbType.Int);
param2.Value = 1;
RunProc("test",param2);
}

No comments:

Post a Comment