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 6634009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:52:47+00:00 2026-05-25T22:52:47+00:00

I’m wondering how to return the result from a SELECT COUNT statement in C#.

  • 0

I’m wondering how to return the result from a SELECT COUNT statement in C#.

I have a sql statement that returns the count of 15.

Currently, I’m returning the datareader. Can I somehow return the result of that as a string?

    static public SqlDataReader FillDataReader(string sql, SqlParameter[] parms)
{
    SqlConnection conn = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand(sql, conn);
    SqlDataReader dr = null;
    conn.Open();
    cmd.CommandTimeout = 120; //120 seconds for the query to finish executing

    foreach (SqlParameter p in parms)
    {
        cmd.Parameters.Add(p);
    }
    try
    {
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    }
    catch (SqlException ex)
    {
        if (dr != null)
        {
            dr.Close();
        }
        conn.Close();


        //DBUtilExceptionHandler(ex, sql);

        throw ex;


    }
    finally
    {
    }

    return dr; //This could be null...be sure to test for that when you use it

}

Or I could use a different method. I just don’t know what it should be.

Any help is appreciated.

This is my select statement:

        select count(LeadListID) from LeadLists WHERE SalesPersonID = 1
AND LeadListDateCreated BETWEEN '9/1/11' AND '10/1/11 23:59:59'
  • 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-05-25T22:52:48+00:00Added an answer on May 25, 2026 at 10:52 pm

    Sure – just use:

    int count = (int) query.ExecuteScalar();
    // TODO: Decide the right culture to use etc
    return count.ToString();
    

    Notes:

    • Use using statements instead of manual try/catch/finally blocks
    • You should close the connection whether or not there was an error
    • Given that the natural result of the query is an integer, I would change it to return an int, not a string. Let the caller make that conversion if they want to
    • If there’s an error, you should almost certainly let the exception bubble up, rather than returning null

    I would write the code as:

    public static int ExecuteScalarInt32(string sql, SqlParameter[] parms)
    {
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        using (SqlCommand command = new SqlCommand(sql, conn) { Parameters = parms })
        {
            conn.Open();
            command.CommandTimeout = 120;
            return (int) command.ExecuteScalar();
        }
    }
    

    If you really needed a version to work on an arbitrary data reader, you could write it as:

    public static T ExecuteQuery<T>(string sql, SqlParameter[] parms,
                                    Func<SqlDataReader, T> projection)
    {
        using (SqlConnection conn = new SqlConnection(ConnectionString))
        using (SqlCommand command = new SqlCommand(sql, conn) { Parameters = parms })
        {
            conn.Open();
            command.CommandTimeout = 120;
            return projection(command.ExecuteReader());
        }
    }
    

    And then call it with:

    int count = ExecuteQuery<int>(sql, parms, reader => {
                                      if (!reader.MoveNext()) {
                                          throw new SomeGoodExceptionType("No data");
                                      }
                                      return reader.GetInt32(0);
                                  });
    
    • 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 have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.