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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:07:42+00:00 2026-05-16T16:07:42+00:00

Suppose i have this sql statement and I have executed a sql command to

  • 0

Suppose i have this sql statement and I have executed a sql command to get a datareader:

"select 1 union select 2"
//.....
var rdr = cmd.ExecuteReader();

and now i want to read the value in the first column of the first row:

var myInt = (int)rdr.GetValue(0); //great this works
var myLong = (long)rdr.GetValue(0); //throws cast exception, even though you can cast int to long

So it appears the type you cast to in C# needs to match exactly the SQL type. I.E. If the sql type is bigint, you can only cast to long. If the sql type is int, you can only cast to int. No mix and match…

I just want to get something that works regardless of the type of integer c# asks for and sql returns, as long as you could theoretically cast one to the other. So if SQL Server gives me a floating point type, and I’m asking for an int, I want the truncated int you get from doing that cast.

My goal is to make this work with generics, so I can have this function work when the generic parameter doesn’t exactly match the datatype in sql server:

List<T> GetFirstColumn<T>(string sql) where T : struct
{
    //get connection, execute reader
    // loop: 
    //    lst.Add(  (T) rdr.GetValue(0));
}

I’d like this to work for both statments:

var sql1 = "Select 1"; //sql int
var sql2 = "Select cast(1 as bigint)"; //sql equivalent of a long
var lst1 = GetFirstColumn<int>(sql1);
var lst2 = GetFirstColumn<int>(sql2);

Does anyone have a relatively painless way of doing this?

  • 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-16T16:07:42+00:00Added an answer on May 16, 2026 at 4:07 pm

    System.Convert will take care of the conversion.

    T GetValue<T>(SqlDataReader rdr)
    {
        var dbVal = rdr.GetValue(0);
        var csVal = (T)System.Convert.ChangeType(dbVal, typeof(T));
    }
    

    Caveat: if T == Nullable<S>, you need to do some extra work with reflection to get the underlying type and call ChangeType with typeof(S) as the type parameter. Apparently, MS didn’t update the ChangeType function with .NET 2.0 and the introduction of nullables. And if it’s a nullable, and dbVal is DBNull, you can just return null.

    object dbValue = 5;
    
    //this throws
    Convert.ChangeType(dbValue, typeof(int?));
    
    //this works
    if(dbValue == DBNull.Value || dbValue == null) 
    {
      if(typeof(int?).IsNullable) //or is a class, like string
      {return null;}
    
      dbValue = null;
    }
    
    var type = GetUnderlyingType<int?>(); //== typeof(int)
    Convert.ChangeType(dbValue, type);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 535k
  • Answers 535k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think you need to grant the privileges to 'monty'@'localhost',… May 17, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer Well, Concat certainly should work: // f3 will be IEnumerable<FileInfo>… May 17, 2026 at 1:06 am
  • Editorial Team
    Editorial Team added an answer This can be cleaned up quite a lot, but it… May 17, 2026 at 1:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i want help how to solve this sql problem. suppose i have 3 tables
Suppose I have this data: Employee Task IsCurrent -------- ---- --------- Jeff 1 Yes
Suppose we have this table.. CREATE TABLE `appointments` ( `idappointments` int(11) NOT NULL AUTO_INCREMENT,
Suppose i have the following 2 SQL tables: Foo Column DataType --------------------------- Title NVARCHAR(20)
This one i had today is a strange one. I have this query in
It looks like #temptables created using dynamic SQL via the EXECUTE string method have
I know there have been numerous questions here about inline sql vs stored procedures...
Suppose I have a table a with one column b and three rows(1,2,3), I
Suppose you have a table: CREATE TABLE Customer ( batch_id NUMBER, customer_name VARCHAR2(20), customer_address
Suppose i have two tables declare @emp table ( EmpID int, EmpName varchar(10) )

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.