A
andy.goss
I have written a class to handle my sql server connections. I am not
sure that I am taking the best approach, however I am running into a
specific problem. In a console app that I am developing, i create a
new instance of the class and then call a method that executes a stored
procedure based on the stored procedure name that I pass it and an
ArrayList of SqlParameter objects containing the data that I need to
add. This works beautifuly the first time I call it. The stored
procedure is called and the data is inserted into the table. However,
the next call from my main app causes it to give the error message that
the parameter is contained by another SqlParameterCollection. I have
tried to clear the SqlCommand.Parameters however when I debugged and
looked, the commad has no parameters. Does any one have any
suggestions for how to solve this problem or a suggestion on a sql
server connection handler class? I have included the code from my
class for your reference. I appreciate any feedback. thanks.
public void executeSP(string spName, ArrayList parameters)
{
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = spName;
sqlCmd.Parameters.Clear();
foreach(SqlParameter param in parameters)
{
sqlCmd.Parameters.Add(param);
}
try
{
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
}
catch(Exception e)
{
Console.WriteLine("SQL Error: " + e.Message);
throw new Exception(e.Message, e.InnerException);
}
}
sure that I am taking the best approach, however I am running into a
specific problem. In a console app that I am developing, i create a
new instance of the class and then call a method that executes a stored
procedure based on the stored procedure name that I pass it and an
ArrayList of SqlParameter objects containing the data that I need to
add. This works beautifuly the first time I call it. The stored
procedure is called and the data is inserted into the table. However,
the next call from my main app causes it to give the error message that
the parameter is contained by another SqlParameterCollection. I have
tried to clear the SqlCommand.Parameters however when I debugged and
looked, the commad has no parameters. Does any one have any
suggestions for how to solve this problem or a suggestion on a sql
server connection handler class? I have included the code from my
class for your reference. I appreciate any feedback. thanks.
public void executeSP(string spName, ArrayList parameters)
{
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = spName;
sqlCmd.Parameters.Clear();
foreach(SqlParameter param in parameters)
{
sqlCmd.Parameters.Add(param);
}
try
{
sqlCon.Open();
sqlCmd.ExecuteNonQuery();
sqlCon.Close();
}
catch(Exception e)
{
Console.WriteLine("SQL Error: " + e.Message);
throw new Exception(e.Message, e.InnerException);
}
}