How do I cast this code to avoid errors?

Hi experts,

When I run the following code as is and enter values, it works:

                For Each row As DataRow In dtCurrentTable.Rows

                    Dim txtsourcen As String = TryCast(row.ItemArray(1), String)
                    Dim txtsourcea As String = TryCast(row.ItemArray(2), String)
                    Dim txtsourcei As Decimal = TryCast(row.ItemArray(3), String)
                    Dim SourceAmount As String = String.Format("{0:C}", txtsourcei)

                    output = "<span style='font-weight:bold;color:#3b3b3b;font-size:12px'>Name: </span>" & txtsourcen & "<br /><span style='font-weight:bold;color:#3b3b3b;font-size:12px'>Address: </span>" & txtsourcea & "<br /><span style='font-weight:bold;color:#3b3b3b;font-size:12px'>Income:</span> " & SourceAmount & "<br />"

                    lblPreviewSourceName.Text += output & "<br />"

                    'lblPreviewSourceName.Text += txtsourcen & "<br />"
                    'lblPreviewSourceAddress.Text += txtsourcea & "<br />"
                    'lblPreviewIncomeSource.Text += txtsourcei & "<br />"

                    'get the values from the TextBoxes

                    'then add it to the collections with a comma "," as the delimited values

                    rowIndex += 1

Open in new window

However, if left blank,

I throws the following error:

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

How do I cast it to avoid this error when left blank?

Thanks in advance
LVL 29
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Change the line to this.
Dim txtsourcei As Decimal = If(Not String.IsNullOrWhiteSpace(row.ItemArray(3)), TryCast(row.ItemArray(3), String), 0.0)

Open in new window

HainKurtSr. System AnalystCommented:
you can pad it with 0 before using TryCast

Dim txtsourcei As Decimal = TryCast(row.ItemArray(3), String)

Open in new window


Dim txtsourcei As Decimal = TryCast("0" & row.ItemArray(3), String)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sammySeltzerAuthor Commented:
Thanks to both of you.

Both appear to work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.