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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:16:28+00:00 2026-06-05T17:16:28+00:00

Question: I wrote a method to retrieve a SQL result as a list of

  • 0

Question:

I wrote a method to retrieve a SQL result as a list of a class instead of a datatable.
The problem is, I have a int field in the database, which is nullable.

If I hit a row with a NULL int, DataReader returns DbNull.Value instead of null.
So System.Convert.ChangeType(objVal, fi.FieldType) throws an exception, because it can’t convert DbNull to an int.

So far so bad.
I thought I had solved the problem, when I just compared objVal to DbNull.Value and if true, did this instead:
System.Convert.ChangeType(null, fi.FieldType)

unfortunately, I just realized, the resulting integer type is 0 instead of NULL.

So I just tried changing the int type in my class to Nullable<int>, but now I have the problem that when a value is not DbNull.Value, ChangeType throws an exception because it can’t convert int to nullable<int>…

So now I try to detect the type of the object returned by datareader, and convert it to a nullable value.

tTypeForNullable is correctly shown as Nullable<int>.
But when I look at the result type, I get: int.

Why is that ? And more important: How can I do that properly ?

Please note that because type is an object, I can’t use a generic method to create Nullable<int>.

bool bisnull = IsNullable(objVal);
bool bisnullt = IsNullable(fi.FieldType);

if (bisnullt)
{
    Type tTypeForNullable = typeof(Nullable<>).MakeGenericType(objVal.GetType());

    //object result = Activator.CreateInstance(tTypeForNullable, new object[] { objVal });
    //object result = Activator.CreateInstance(typeof(Nullable<int>), new object[] { objVal });
    object result = Activator.CreateInstance(tTypeForNullable, objVal);
    Type tres = result.GetType();
    fi.SetValue(tThisValue, System.Convert.ChangeType(result, fi.FieldType));
}

Here’s the complete routine for reference:

public virtual System.Collections.Generic.IList<T> GetList<T>(System.Data.IDbCommand cmd)
        {
            System.Collections.Generic.List<T> lsReturnValue = new System.Collections.Generic.List<T>();
            T tThisValue = default(T);
            Type t = typeof(T);

            lock (cmd)
            {
                using (System.Data.IDataReader idr = ExecuteReader(cmd))
                {

                    lock (idr)
                    {

                        while (idr.Read())
                        {
                            //idr.GetOrdinal("")
                            tThisValue = Activator.CreateInstance<T>();

                            // Console.WriteLine(idr.FieldCount);
                            for (int i = 0; i < idr.FieldCount; ++i)
                            {
                                string strName = idr.GetName(i);
                                object objVal = idr.GetValue(i);


                                System.Reflection.FieldInfo fi = t.GetField(strName);
                                //Type tttttt = fi.FieldType;
                                if (fi != null)
                                {
                                    //fi.SetValue(tThisValue, System.Convert.ChangeType(objVal, fi.FieldType));
                                    if (objVal == System.DBNull.Value)
                                    {
                                        objVal = null;
                                        fi.SetValue(tThisValue, null);
                                    }
                                    else
                                    {
                                        //System.ComponentModel.TypeConverter conv = System.ComponentModel.TypeDescriptor.GetConverter(fi.FieldType);

                                        bool bisnull = IsNullable(objVal);
                                        bool bisnullt = IsNullable(fi.FieldType);

                                        if (bisnullt)
                                        {
                                            Type tTypeForNullable = typeof(Nullable<>).MakeGenericType(objVal.GetType());

                                            //object result = Activator.CreateInstance(tTypeForNullable, new object[] { objVal });
                                            //object result = Activator.CreateInstance(typeof(Nullable<int>), new object[] { objVal });
                                            object result = Activator.CreateInstance(tTypeForNullable, objVal);
                                            Type tres = result.GetType();
                                            fi.SetValue(tThisValue, System.Convert.ChangeType(result, fi.FieldType));
                                        }
                                        fi.SetValue(tThisValue, System.Convert.ChangeType(objVal, fi.FieldType));
                                    }
                                }
                                else
                                {
                                    System.Reflection.PropertyInfo pi = t.GetProperty(strName);
                                    if (pi != null)
                                    {
                                        //pi.SetValue(tThisValue, System.Convert.ChangeType(objVal, pi.PropertyType), null);


                                        if (objVal == System.DBNull.Value)
                                        {
                                            objVal = null;
                                            pi.SetValue(tThisValue, null, null);
                                        }
                                        else
                                        {
                                            pi.SetValue(tThisValue, System.Convert.ChangeType(objVal, pi.PropertyType), null);
                                        }


                                    }
                                    // Else silently ignore value
                                } // End else of if (fi != null)

                                //Console.WriteLine(strName);
                            } // Next i

                            lsReturnValue.Add(tThisValue);
                        } // Whend

                        idr.Close();
                    } // End Lock idr

                } // End Using idr

            } // End lock cmd

            return lsReturnValue;
        } // End Function GetList

with this:

public System.Data.IDataReader ExecuteReader(System.Data.IDbCommand cmd)
        {
            System.Data.IDataReader idr = null;

            lock(cmd)
            {
                System.Data.IDbConnection idbc = GetConnection();
                cmd.Connection = idbc;

                if (cmd.Connection.State != System.Data.ConnectionState.Open)
                    cmd.Connection.Open();

                idr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            } // End Lock cmd

            return idr;
        } // End Function ExecuteReader

Please note that because type is an object, I can’t use a generic method to create Nullable<int>.

  • 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-05T17:16:30+00:00Added an answer on June 5, 2026 at 5:16 pm

    You’re boxing – and the result of a boxing operation for a nullable value type is never a boxed value of that type. It’s either null or a non-nullable value type. See MSDN for more information.

    • 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 that I wrote for Project Euler - Problem 36
I Have wrote a question which got a right answer here about emysql encoding.
Two days ago I wrote this question: How can I retrieve an object on
I have a question about the safety of a cast from long to int.
I have a timepicker class and can show the time after picked. My question
Here is the question: write a method that swaps two variables. These two variables
I'm using Java 6. As the question states, how would I write a method
Based on the accepted answer to this question I wrote the following code: NSData*
Ok, I wrote this question up earlier today but I decided to delete it
So I wrote this simple console app to aid in my question asking. What

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.