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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:15:06+00:00 2026-05-23T23:15:06+00:00

I am trying to pull values from a datatable object and populate an object

  • 0

I am trying to pull values from a datatable object and populate an object for a webservice call dynamically, I have tried a few aproaches but narrowed them down to this, what it seems to be missing is the ability to reflect the target type and cast the object from the data table into one.

I’m scratching my head a lot here!

foreach (PropertyInfo pi in zAccount)
                {
                    object o = row[pi.Name];
                    if (o.GetType() != typeof(DBNull))
                    {
                        pi.SetValue(a, o, null);
                    }
                 }

This gives me type conversion errors:

Object of type ‘System.String’ cannot be converted to type ‘System.Nullable`1[System.Boolean]’.

So the ideal would be something like this:

foreach (PropertyInfo pi in zAccount)
                {
                    object o = typeof(pi.GetType())row[pi.Name];
                    pi.SetValue(a, o, null);
                 }
  • 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-23T23:15:08+00:00Added an answer on May 23, 2026 at 11:15 pm

    Here is a piece of code that I use to do exactly what you are trying to do; convert types out of the DB. Normally you could use Convert.ChangeType, but that doesn’t work right with nullable types, so this method handles that case.

    /// <summary>
    /// This wrapper around Convert.ChangeType was done to handle nullable types.
    /// See original authors work here: http://aspalliance.com/852
    /// </summary>
    /// <param name="value">The value to convert.</param>
    /// <param name="conversionType">The type to convert to.</param>
    /// <returns></returns>
    public static object ChangeType(object value, Type conversionType)
    {
      if (conversionType == null)
      {
        throw new ArgumentNullException("conversionType");
      }
      if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
      {
        if (value == null)
        {
          return null;
        }
        NullableConverter nullableConverter = new NullableConverter(conversionType);
        conversionType = nullableConverter.UnderlyingType;
      }
      return Convert.ChangeType(value, conversionType);
    }
    

    You would then use it like:

    foreach (PropertyInfo pi in zAccount)
    {
      object o = ChangeType(row[pi.Name], pi.GetType());
      pi.SetValue(a, o, null);
    }
    

    Edit:

    Actually, re-reading your post, your error message

    Object of type ‘System.String’ cannot be converted to type ‘System.Nullable`1[System.Boolean]’.

    makes it look like the type that you got back from the database is a string, but the property is of type bool? (nullable boolean) hence it can’t convert it.

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

Sidebar

Related Questions

I'm trying to pull two values from this Dictionary, But the values I'm getting
I'm trying to figure out how pull select values from a table column, but
I have been have issues trying to retrieve the values from a treeMap that
I've generated some JSON and I'm trying to pull it into an object in
I'm trying to use SharpZipLib to pull specified files from a zip archive. All
I would like to pull artifacts from teamcity. I've been trying to use c#
I'm trying to make a pull down menu post a form when the user
I'm trying to write a query that will pull back the two most recent
I am trying to write a function that will pull the name of a
I am trying come up with a way to pull the tables out of

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.