• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

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)
0
JessyRobinson1234
Asked:
JessyRobinson1234
  • 3
  • 2
1 Solution
 
Pratima PharandeCommented:
try like this

 If (IsDBNull(rdr("Actual_Net_Revenue")) = False) Then
                   If (rdr("Actual_Net_Revenue").ToString <> "") Then
                    Me.lblANR.Text = FormatNumber(rdr("Actual_Net_Revenue").ToString, 0)
              End If
                End If
0
 
David H.H.LeeCommented:
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)
0
 
vkarumbaiahCommented:
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)
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
David H.H.LeeCommented:
Hi vkarumbaiah,
Is that any reason you excluded my posted solution as you mentioned IIF() is work better?
0
 
vkarumbaiahCommented:
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.
0
 
David H.H.LeeCommented:
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.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now