Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Question: Referencing Formview label when no data is present

Reply
Thread Tools

Question: Referencing Formview label when no data is present

 
 
VB Programmer
Guest
Posts: n/a
 
      10-22-2007
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!



 
Reply With Quote
 
 
 
 
Brian Williams
Guest
Posts: n/a
 
      11-07-2007
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





"VB Programmer" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
>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!
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OS present but not present. XPD NZ Computing 4 04-11-2007 11:22 PM
Referencing Nested Formview inside an Empty Data Template SK101 ASP .Net 0 10-11-2006 12:32 PM
FormView/Repeater/ Eval(FormView data) David Thielen ASP .Net Web Controls 2 06-16-2006 07:56 PM
set visibility of label in datagrid IF text string present footballhead@gmail.com ASP .Net 3 04-10-2006 10:12 PM
Schema: express that "@a present if and only if @b present", where @a, @b are attributes Ralf Wahner XML 5 12-24-2003 11:37 AM



Advertisments