Question: Referencing Formview label when no data is present

Discussion in 'ASP .Net' started by VB Programmer, Oct 22, 2007.

  1. 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!
    VB Programmer, Oct 22, 2007
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%...
    >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!
    >
    >
    >
    Brian Williams, Nov 7, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    433
  2. Ralf Wahner
    Replies:
    5
    Views:
    620
    Bob Foster
    Dec 24, 2003
  3. SK101
    Replies:
    0
    Views:
    2,778
    SK101
    Oct 11, 2006
  4. Cirene
    Replies:
    3
    Views:
    358
    Cirene
    Nov 4, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    252
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page