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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:48:41+00:00 2026-06-10T18:48:41+00:00

I have the following function: public T GetScalar<T>(string sql, T defaultValue, SQLParam[] sqlParams =

  • 0

I have the following function:

public T GetScalar<T>(string sql, T defaultValue, SQLParam[] sqlParams = null)
{
    DataTable dt = GetDataTable(sql, sqlParams);
    if (dt.Rows.Count == 0) return defaultValue;
    else
    {
        try
        {
            object tmp = dt.Rows[0][0];
            return (T)tmp;
        }
        catch { return defaultValue; }
    }
}

I am getting an InvalidCastException.

During debugging, I can see the value of tmp is 3, and T is of type int.

What is the problem here?

EDIT

A bit of background: This used to work in MySQL. I have now moved to SQLServer (with little effort thankfully) but this code now fails.

  • 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-10T18:48:43+00:00Added an answer on June 10, 2026 at 6:48 pm

    I suspect the value is actually a long, or something like that – and when you unbox, you have to unbox to the exact type (there are a few oddities around signedness and enums, but not long vs int).

    You should be able to see this if you put a watch on tmp.GetType() – or just work out what type it should be from the SQL.

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

Sidebar

Related Questions

I have the following function: public DataTable GetRecordSet(String query, string[] parameters) It is typically
I have the following function: public static byte[] StringToByte(string str) { int length =
I have the following function for SQLite select statement public Boolean itemFound(String cartId,Long ItemId){
I have the following function: public void RestoreDatabase(String databaseName, String backUpFile, String serverName, String
I have the following function in C#: public string getRowKey(string topic, string rk) {
I have the following function: public function updateCustomerInternetBanking($value, $column_to_go_by) { $sql = UPDATE customer
I have the following function: public ActionResult Index(string caller_id, int? id) { Now I
I have following function in my code public List<string> GetpathsById(List<long> id) { List<string> paths
I have written following function public void TestSB() { string str = The quick
I have the following function: Public Function CheckHasRoom(people_id As String, semester As String, year

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.