Link to home
Start Free TrialLog in
Avatar of Joseph Krausz
Joseph Krausz

asked on

Trap dbnull in OledbCommand ExecuteScalar

Hi,
i am trying to connect to a table in access combining a few fields including a currency field, seems to be when asking for the currency field but having no vaklue it returns a DBnull Exception, so my question is how i can parse the field before sending it and howi can get over it,

thanks in advance,
Avatar of kaufmed
kaufmed
Flag of United States of America image

Check for DBNull before you try and assign your data.

e.g.

OleDbCommand cmd = // initialized
decimal currencyValue;

object result = cmd.ExecuteScalar();

if (result != DBNull.Value)
{
    currencyValue = (decimal)result;
}

Open in new window

Avatar of Joseph Krausz
Joseph Krausz

ASKER

this will not work as the sql command includes a few fields including non decimal field so my question is if its possible to parse or trap before executing the sql like nz(amount,0) in access, or it does not have any functions to trap
Can you show your query?
I oriented  myself that using parameters in the SQL string instead of just field names will help me but still didn't tested,
As I'm not so familiar with it please help me out
SQL= "select firstname+' ' +lastname+' '+amount from members";
I assume to use the oledbparameters class to assign the field name after testing the value
So should be like select@firstname...
But I can still use some help for completing it
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial