Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024

  • Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
  • VBForums
  • .NET and More
  • C#
  • 3.0/LINQ [RESOLVED] The SqlParameter is already contained by another SqlParameterCollection

  1. Jul 21st, 2011, 12:53 AM

    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Thread Starter Frenzied Member
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024


    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    [RESOLVED] The SqlParameter is already contained by another SqlParameterCollection
    C# Code:

  1. var items = db.Database.SqlQuery("Sproc1 @Param1", new SqlParameter("Param1", param1_value));
  2. foo_sum = items.Sum(x => x.Foo);
  3. bar_sum = items.Sum(x => x.Bar);

The sproc returns a list of three int fields, and I want to get the sum total of two of the fields. When I run this, I'm getting the error "The SqlParameter is already contained by another SqlParameterCollection." on the last line. How do I fix this to do it correctly? I'm using EntityFramework DbContext.

-
  1. Jul 21st, 2011, 06:38 PM

    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Thread Starter Frenzied Member
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
    Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024

    -

    Re: The SqlParameter is already contained by another SqlParameterCollection

    Fixed it by assigning "items" to a List() and doing the Sum() calculations on the List instead of "items". ---
  • Lỗi the sqlparameter is already contained by another sqlparametercollection năm 2024
  • VBForums
  • .NET and More
  • C#
  • 3.0/LINQ [RESOLVED] The SqlParameter is already contained by another SqlParameterCollection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  • BB code is On
  • Smilies are On
  • code is On
  • code is On
  • HTML code is Off

Forum Rules

Click Here to Expand Forum to Full Width

The code itself was simple connecting to the database, executing two queries using different SqlCommand objects, but the same SqlParameter collection and returning the results to the user.

The method itself look two parameters, a SQL query as a string and a SQL Parameter[]. I would then simply add the parameters to the SqlCommand:

SqlCommand sqlCmd = new SqlCommand(sql, sqlConn); sqlCmd.Parameters.AddRange(queryParams);

After executing I would close the connection, then code would do some other stuff and a different query would be executed but using the same parameter collection. In theory, there is no reason why this wouldn’t work however the code was throwing an exception with the error “The SqlParameter is already contained by another SqlParameterCollection.” How odd, I had closed the connection and it shouldn’t be referred so I should be able to add it.

Turns out, or at least I think, that the garage collection isn’t kicking in quick enough and so the reference is still alive. Quick fix was to clear the parameters on the SqlCommand object after execution.