Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DataSet does not support System.Nullable

DataSet does not support System.Nullable

Scheduled Pinned Locked Moved C#
javabusinesshelpquestionlearning
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Brady Kelly
    wrote on last edited by
    #1

    I am trying to build a DataTable from a TList, which is a collection of T business objects. I am using reflection to get all the properties of T, and then loop through them creating DataColumn objects. However, I get an error when I try and create a new column where a property of T is nullable. Is there a quick way around this? Looking at the type name for a nullable property of T is also pretty confusing, as the type name is "Nullable`1".

    "A little learning is a dangerous thing; drink deep, or taste not the Pierian spring: there shallow draughts intoxicate the brain, and drinking largely sobers us again.", by Alexander Pope My New Blog

    L 1 Reply Last reply
    0
    • B Brady Kelly

      I am trying to build a DataTable from a TList, which is a collection of T business objects. I am using reflection to get all the properties of T, and then loop through them creating DataColumn objects. However, I get an error when I try and create a new column where a property of T is nullable. Is there a quick way around this? Looking at the type name for a nullable property of T is also pretty confusing, as the type name is "Nullable`1".

      "A little learning is a dangerous thing; drink deep, or taste not the Pierian spring: there shallow draughts intoxicate the brain, and drinking largely sobers us again.", by Alexander Pope My New Blog

      L Offline
      L Offline
      Lisa Jorgensen
      wrote on last edited by
      #2

      You need to get the underlying type, e.g.:

      // Get PropertyInfo[] properties from T
      foreach (PropertyInfo propInfo in properties)
      {
      Type propType = propInfo.PropertyType;
      if (propType.IsGenericType &&
      propType.GetGenericTypeDefinition() == typeof(Nullable<>))
      {
      propType = Nullable.GetUnderlyingType(propType);
      }
      // Create DataColumn from propType, etc. ...
      }

      B W 2 Replies Last reply
      0
      • L Lisa Jorgensen

        You need to get the underlying type, e.g.:

        // Get PropertyInfo[] properties from T
        foreach (PropertyInfo propInfo in properties)
        {
        Type propType = propInfo.PropertyType;
        if (propType.IsGenericType &&
        propType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
        propType = Nullable.GetUnderlyingType(propType);
        }
        // Create DataColumn from propType, etc. ...
        }

        B Offline
        B Offline
        Brady Kelly
        wrote on last edited by
        #3

        Thanks Lisa, I actually did discover that a while after posting. I should have updated. :-O

        1 Reply Last reply
        0
        • L Lisa Jorgensen

          You need to get the underlying type, e.g.:

          // Get PropertyInfo[] properties from T
          foreach (PropertyInfo propInfo in properties)
          {
          Type propType = propInfo.PropertyType;
          if (propType.IsGenericType &&
          propType.GetGenericTypeDefinition() == typeof(Nullable<>))
          {
          propType = Nullable.GetUnderlyingType(propType);
          }
          // Create DataColumn from propType, etc. ...
          }

          W Offline
          W Offline
          WiiMaxx
          wrote on last edited by
          #4

          instead of your

          if (propType.IsGenericType &&
          propType.GetGenericTypeDefinition() == typeof(Nullable<>))
          {
          propType = Nullable.GetUnderlyingType(propType);
          }

          you could also do

          column.DataType = Nullable.GetUnderlyingType(PropertyType) ?? PropertyType;

          depending on your knowledge about the ?? operator and your preferences it could improve the readability

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups