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. Set Default Value of DataTable field When Retrieving from Database

Set Default Value of DataTable field When Retrieving from Database

Scheduled Pinned Locked Moved C#
databasehelplearning
3 Posts 2 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.
  • P Offline
    P Offline
    polishprogrammer
    wrote on last edited by
    #1

    I have an application that needs to retrieve data from a database using a left join query. The left join, of course, may return null values from the "right" database table (call it table2) if a matching record is not found in table2. This can cause a problem because I have fields on my form bound to the values in the DataTable filled by the SQL query. I handled this problem by running through each line in the resultant DataTable and replacing any null values with an appropriate default, but this operation is relatively time consuming. Is there a better method to set the default values on the DataTable. Thanks.

    B 1 Reply Last reply
    0
    • P polishprogrammer

      I have an application that needs to retrieve data from a database using a left join query. The left join, of course, may return null values from the "right" database table (call it table2) if a matching record is not found in table2. This can cause a problem because I have fields on my form bound to the values in the DataTable filled by the SQL query. I handled this problem by running through each line in the resultant DataTable and replacing any null values with an appropriate default, but this operation is relatively time consuming. Is there a better method to set the default values on the DataTable. Thanks.

      B Offline
      B Offline
      Ben Fair
      wrote on last edited by
      #2

      Yes, in the query use the ISNULL() or COALESCE() SQL Server functions and provide the default values that you want there, then you don't have to do anything with the resultset in code.

      Keep It Simple Stupid! (KISS)

      P 1 Reply Last reply
      0
      • B Ben Fair

        Yes, in the query use the ISNULL() or COALESCE() SQL Server functions and provide the default values that you want there, then you don't have to do anything with the resultset in code.

        Keep It Simple Stupid! (KISS)

        P Offline
        P Offline
        polishprogrammer
        wrote on last edited by
        #3

        Thanks. The application I'm working with uses Access, so I think I need to use IIF with ISNULL() to make it work with this software, but that's exactly the idea I was looking for. I wanted something that could work quickly, without iterating through the rows of the table or doing anything else in the code. Thanks.

        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