Link to home
Start Free TrialLog in
Avatar of JessyRobinson1234
JessyRobinson1234

asked on

Conversion from string "" to type 'Double' is not valid.

I get this error only when there is no value. How can I program it where it won't throw the error in my VB code

 Me.lblANR.Text = FormatNumber(rdr("Actual_Net_Revenue").ToString, 0)
ASKER CERTIFIED SOLUTION
Avatar of Pratima
Pratima
Flag of India 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
Hi JessyRobinson1234,
>>..How can I program it where it won't throw the error in my VB code

Alternatively, you can use IIF() function.
http://msdn.microsoft.com/en-us/library/27ydhh0d(vs.71).aspx

eg:
Me.lblANR.Text = FormatNumber(IIF(rdr("Actual_Net_Revenue").ToString="",0,rdr("Actual_Net_Revenue").ToString), 0)
I think
rdr("Actual_Net_Revenue").ToString="" could throw an error if the value of rdr("Actual_Net_Revenue") is dbnull
this may work better
 
Me.lblANR.Text = FormatNumber(IIf(String.IsNullOrEmpty(rdr("Actual_Net_Revenue")), 0, rdr("Actual_Net_Revenue").ToString), 0)
Hi vkarumbaiah,
Is that any reason you excluded my posted solution as you mentioned IIF() is work better?
Hi x_com
As you can see I was not excluding your solution, Instead I was clarifying and adding to it.
rdr("Actual_Net_Revenue").ToString could throw an error if the value of rdr("Actual_Net_Revenue") is DBNull. String.IsNullOrEmpty(rdr("Actual_Net_Revenue")) will fix that problem.
Hope this makes it clear.
Hi vkarumbaiah,
Ok, understood but i though should have points splits for the assistance. FYI, if you put .ToString() for the value that returned from db, you can bypass the DBNULL problem since it already converted to string type.
http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx.
Anyway, i'm glad you resolved your glitches here. Cheers.