Set Default Value of DataTable field When Retrieving from Database
-
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.
-
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.
-
Yes, in the query use the
ISNULL()
orCOALESCE()
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)
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.