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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:57:03+00:00 2026-06-06T11:57:03+00:00

I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params

  • 0

I have the following method:

public static T ExecuteScalar<T>(
    string query, 
    SqlConnection connection, 
    params SqlParameter[] parameters) where T : new()
{
    // Create SqlCommand
    SqlCommand command = CreateCommand(query, connection, parameters);

    // Execute command using ExecuteScalar
    object result = command.ExecuteScalar();

    // Return value as expected type
    if (result == null || result is DBNull) return default(T);
    return (T)result;
}

I want to have the MIN_ACTIVE_ROWVERSION of the database as an ulong. The strange thing is.. the First method call below generates an error but the second method call works fine.

Method call 1 generates an error:

ulong minActiveRowversion = 
    SqlUtils.ExecuteScalar<ulong>(
        "SELECT CAST(MIN_ACTIVE_ROWVERSION() AS BIGINT)"
        , _connectionString);

Error:

System.InvalidCastException: Specified cast is not valid.

Method call 2 works fine:

ulong minActiveRowversion = 
    (ulong)SqlUtils.ExecuteScalar<long>(
        "SELECT CAST(MIN_ACTIVE_ROWVERSION() AS BIGINT)"
        , _connectionString);

I don’t understand how that is possible because the result of the command.ExecuteScalar() method is this:

object result       | 1955612
result.GetType()    | {Name = "Int64" FullName = "System.Int64"}
  1. Can someone tell me why the first scenario is not possible and the second scenario works?
  2. Can someone tell me how I can solve it so I can use scenario 1.
  • 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-06T11:57:05+00:00Added an answer on June 6, 2026 at 11:57 am

    Why

    You can only unbox a value type to it’s original type. In your case, the cast first needs to go to long from object and then to ulong.

    See this question for more detail:

    Why can't I unbox an int as a decimal?

    It also links a blog post by Eric Lippert.

    How

    One way, as you know, is to cast to the original type before casting to T – unless, of course, the original type is T.

    As mentioned in the comments, another way is to use conversion routines (Convert.ToUInt64) and not explicit casting.

    This could potentially be achieved using a Func<object, T>:

    public static T ExecuteScalar<T>(
        Func<object, T> conversionFunctor,
        string query, 
        SqlConnection connection, 
        params SqlParameter[] parameters) where T : new()
    {
        // Create SqlCommand
        SqlCommand command = CreateCommand(query, connection, parameters);
    
        // Execute command using ExecuteScalar
        object result = command.ExecuteScalar();
    
        // Return value as expected type
        if (result == null || result is DBNull) 
            return default(T);
    
        return conversionFunctor(result);
    }
    

    Making your call:

    ulong minActiveRowversion = 
        SqlUtils.ExecuteScalar<ulong>(
            Convert.ToUInt64,
            "SELECT CAST(MIN_ACTIVE_ROWVERSION() AS BIGINT)"
            , _connectionString);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following method public static void SerializeToXMLFile(Object obj,Type type, string fileName) {
I have the following method declaration: public static bool SerializeObject<T>(string filename, T objectToSerialize){ I
On my front end, I have following functions: method to encrypt: public static string
That is, I have a method such as the following: public static int CreateTaskGroup(string
If i have the following method - public static void C() { Connection con
I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have the following method signature: public static void InvokeInFuture(Delegate method, params object[] args)
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string
I have the following method: public static string ByteToString(byte[] Bytes, int Length) { Debug.Assert(Length
I have the following extension method public static T Field<T>(this DataRow row, string columnName)

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.