Search code examples
sqlms-accesscastingnulloledbdatareader

How can I preempt a "Specified cast is not valid" exception?


In querying an MS Access database in a Web API app, I get a "Specified cast is not valid" exception if I'm trying to assign an empty string to a variable. IOW, when this code:

var accountID = oleDbD8aReader.GetInt16(0); 
var platypusName = oleDbD8aReader.GetString(1); 

...reaches a record where the second column in the result set contains an empty/null string, it bombs.

So, I thought I could head that off at the pass like this:

string platypusName;
var accountID = oleDbD8aReader.GetInt16(0); 
if (!string.IsNullOrEmpty(oleDbD8aReader.GetString(1)))
{
    platypusName = oleDbD8aReader.GetString(1); 
}
else
{
    platypusName = string.Empty;
}

...but it doesn't work - I still get a "Specified cast is not valid" exception.

How can I safely check for an empty string/null value there and ignore that pass through the result set so that it will get subsequent records?

Or can I preclude this by changing the SQL statement to exclude empty/null strings from the result set? If so, how? The query is in this format:

SELECT td_duckbill_accounts.platypus_no, t_accounts.name 
FROM t_accounts 
INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no      
ORDER BY td_duckbill_accounts.platypus_no

Solution

  • I think having the query return empty string is easy solution:

    SELECT td_duckbill_accounts.platypus_no, 
           IIF(ISNULL(t_accounts.name),'',t_accounts.name) AS name
    FROM t_accounts 
    INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no      
    ORDER BY td_duckbill_accounts.platypus_no
    

    This should also work but I can't test it right now:

    SELECT td_duckbill_accounts.platypus_no, 
           Nz(t_accounts.name,'') AS name
    FROM t_accounts 
    INNER JOIN td_duckbill_accounts ON t_accounts.account_no = td_duckbill_accounts.account_no      
    ORDER BY td_duckbill_accounts.platypus_no