0

I made a helper method for calling my queries in async and I added a retry counter of 5 just in case. It work most of the times but i still get all 5 tries with:

The SqlParameter is already contained by another SqlParameterCollection.

I call it from several places expecting one item or a list of items and with one or 2 params max.

my code

public static Object GetQueryResult(List<SqlParameter> parameters, Type dataType, string query, bool isList) {
    Object result = null;
    int retry = 0;
    while (result == null && retry < 5) {
        retry++;
        try {
            if (isList) {
                if (parameters.Count > 1)
                    result = lnssContext.Database.SqlQuery(dataType, query, parameters[0], parameters[1]).Cast<Object>().ToList();
                else
                    result = lnssContext.Database.SqlQuery(dataType, query, parameters[0]).Cast<Object>().ToList();
            } else {
                if (parameters.Count > 1)
                    result = lnssContext.Database.SqlQuery(dataType, query, parameters[0], parameters[1]).Cast<Object>().ToList().FirstOrDefault();
                else
                    result = lnssContext.Database.SqlQuery(dataType, query, parameters[0]).Cast<Object>().ToList().FirstOrDefault();
            }
        } catch (Exception e) {
            log.Info("Query failed . Retry count:" + retry, e);
            result = null;
        }
    }
    return result;
}
3
  • It is Linq-To-SQL isn't it? Commented Jul 2, 2015 at 13:41
  • lnssContext.Database is a System.Data.Entity.Database if that's what you mean. This is c# with mysql. Commented Jul 2, 2015 at 13:44
  • since i was iterating i simply cloned the parameters before calling it again and it worked Commented Jul 3, 2015 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.