Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:42:20+00:00 2026-06-15T15:42:20+00:00

I’ve been trying to get this method to work since yesterday and have taken

  • 0

I’ve been trying to get this method to work since yesterday and have taken to pulling my hair out –

Basically, I have a method that retrieves articles from a database. Below is the method code:

public static IEnumerable<Article> RetrieveTopTenArticles()
{
    using(var dbConn = new SqlConnection(Settings.Instance.DbConnectionString))
    {
        const String query = "SELECT TOP 10 Title, Content FROM Article";
        Func<SqlDataReader, Article> operation = reader => reader.ToArticle();
        return dbConn.SqlRetrieveMultiple(query, operation);
    }
}

And in a separate “Extensions” class –

public static IEnumerable<T> SqlRetrieveMultiple<T>(this SqlConnection sqlConnection, String query, Func<SqlDataReader, T> operation, params SqlParameter[] parameters)
{
    sqlConnection.Open();
    using (var command = new SqlCommand(query, sqlConnection))
    {
        if (parameters.Length > 0)
        {
            command.Parameters.AddRange(parameters);
        }
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return operation.Invoke(reader);
            }
        }
    }
}

As you can see, I have an extension method that performs a SQL select statement, and iterates over the results, invoking the operation and converting each SqlDataReader row into my Article type (also done with an extension method). I believe I’ve used this same exact extension method in the past with no issues.

When I debug this and it gets to the dbConn.SqlRetrieveMultiple(query, operation); row, if I hit F11 to step into, it steps over the row completely. It continues to walk down the chain of ending nested brackets and eventually I get an exception on the sqlConnection.Open().

The exception I get is:

The ConnectionString property has not been initialized.

I have found that the function will step into just fine if I remove my Func delegate. I feel like this has to be something stupid / unrelated to extension methods or SqlConnection because I get the same behavior if I put it in a regular private static method within the same class. If I turn on .NET framework source stepping at the sqlConnection.Open(); line, it jumps into a ctor method of the .NET framework… I can post up more code if requested but I think I’ve got everything necessary. Please let me know if you’ve got any ideas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:42:21+00:00Added an answer on June 15, 2026 at 3:42 pm

    You’re returning the result of your iterator method instead of enumerating it. Iterator methods use deferred execution. As a result, it is not actually doing anything until well after the connection it will use has been disposed. That’s why it “just stepped over” the method and why it failed at some arbitrary later time.

    You can fix this problem in two ways. Either get all of the results ahead of time and return them, eagerly getting the items before disposal occurs, using a method like ToArray:

    return dbConn.SqlRetrieveMultiple(query, operation).ToArray();
    

    or you can ensure disposal is delayed until after the items have been lazily retrieved, by making your outer method an iterator method as well:

    using(var dbConn = new SqlConnection(Settings.Instance.DbConnectionString))
    {
        const String query = "SELECT TOP 10 Title, Content FROM Article";
        Func<SqlDataReader, Article> operation = reader => reader.ToArticle();
        foreach (var e in dbConn.SqlRetrieveMultiple(query, operation))
            yield return e;
    }
    

    However, keep in mind that if users iterate this lazy version twice, they will end up opening two connections. That may or may not be what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I have been unable to fix a problem with Java Unicode and encoding. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.