Question: Referencing Formview label when no data is present

V

VB Programmer

I have a FormView control, "fvDeposits", which has 1 databound label in it,
called "MyTotal". If the label is blank/no data this line of code gets the
error "Run-time exception thrown : System.ArgumentNullException - Object
cannot be null"

CType(CType(Me.fvDeposits.FindControl("MyTotal"), Label).Text, Decimal)

Any ideas how I can get around this?

Thanks!
 
B

Brian Williams

You can't convert null to decimal; you need to check for null first. I don't
know what the VB.Net code should look like, but in C# you could do this.



Label myLabel = (Label) fvDeposits.FinControl("MyTotal");

Decimal myDecimal;

myDecimal = myLabel.Text == null ? Decimal.MinValue : (Decimal)
myLabel.Text;



or you can do:



Label myLabel = (Label) fvDeposits.FinControl("MyTotal");

Decimal myDecimal;

if(!String.IsNullOrEmpty(myLabel.Text))

{

myDecimal = (Decimal) myLabel.Text;

}

else

{

myDecimal = Decimal.MinValue;

}



Regards;

Brian K. Williams
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top