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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:05:53+00:00 2026-06-06T07:05:53+00:00

An SQL aggregate function counting number of name enteries in DB. string cnnStr =

  • 0

An SQL aggregate function counting number of name enteries in DB.

string cnnStr = ConfigurationManager.ConnectionStrings["LGFConnectionString"].ConnectionString;
string mySQL = "SELECT COUNT(*) FROM  " + which.table + " WHERE " + which.column + " = ?pram;";
string value = null;

using (MySqlConnection cnn = new MySqlConnection(cnnStr))
{
        using (MySqlCommand cmd = new MySqlCommand(mySQL, cnn))
        {
            MySqlParameter param = new MySqlParameter("?pram", MySqlDbType.VarChar, 128);
            param.Value = which.text;
            cmd.Parameters.Add(param);

            cnn.Open();

            value = cmd.ExecuteScalar() as string;
            value = cmd.ExecuteScalar().ToString();

            cnn.Close();
        }
}

Notice that I have called cmd.ExecuteScalar twice. Interesting part is that the query returns different results.

value = cmd.ExecuteScalar() as string;

doesn’t return the correct value. It returns null for both if name is present or missing in the name column.

value = cmd.ExecuteScalar().ToString();

returns correctly. This returns “1” if present and “0” if missing.

In searching the web, I haven’t found an understandable explanation.

I have read that if name is missing from name column, then cmd.ExecuteScalar will return null.

What is the difference between:

value = cmd.ExecuteScalar() as string;
value = cmd.ExecuteScalar().ToString();

Thank you,
deDogs

  • 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-06T07:05:55+00:00Added an answer on June 6, 2026 at 7:05 am

    as in docs

    The as operator is used to perform conversions between compatible types.

    The as operator is like a cast except that it yields null on conversion failure instead of raising an exception

    // if cmd.ExecuteScalar() is string then return string
    // if not then return null
    // this will return null, because cmd.ExecuteScalar() won't return string
    // for your code it should return Int32
    value = cmd.ExecuteScalar() as string;
    

    ToString() in docs

    ToString is the major formatting method in the .NET Framework. It
    converts an object to its string representation so that it is suitable
    for display. (For information about formatting support in the .NET
    Framework, see Formatting Types.)

    // return a string that represents the current object
    // will return correct value because it casts Int32 value to string value
    value = cmd.ExecuteScalar().ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I create an aggregate function for string column in SQL Server 2008. C# code
I have an user defined table function in SQL Server that aggregate data from
How to aggregate string( concatenate) with Oracle 10g SQL?
The standard SQL aggregate function max() will return the highest value in a group;
is it possible to alias an aggregate function in a select clause as AliasTable.AliasColumn?
I need to write an aggregate extension function (implemented in C) for mySQL 5.x.
Possible Duplicate: Simulating group_concat MySQL function in MS SQL Server 2005? Does T-SQL have
According to MSDN , Median is not available as an aggregate function in Transact-SQL.
Question: I want to write a custom aggregate function that concatenates string on group
Whenever we use an aggregate function in SQL ( MIN , MAX , AVG

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.