Creating a variable to accept Decimals.

Discussion in 'ASP .Net' started by Phillip Vong, Oct 10, 2006.

  1. Phillip Vong

    Phillip Vong Guest

    I'm using VS2005 and creating a simple test ASPX page in VB.

    I have a simple FORMVIEW1 with a label "YTDLabel" databound to a SQL DB.
    The DB datatype is set to decimal (18,2) and the value of this cell is 16.1.
    Here is my simple Page_Load code and all I want it to do is see if the
    decimal figure is greater than 1, if it is, then hide the YTDLabel. The
    error message I'm getting from VS2005 is that
    FormView1.FindControl("YTDLabel") is underlined and it says "Value fo Type
    'System.Web.UI.Control' can not be converted to 'Decimal'". Can someone
    help me out? I'm just a newbie trying to learn. Thanks!

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim YTDtest As Decimal
    YTDtest = FormView1.FindControl("YTDLabel")

    If YTDtest > 0 Then
    FormView1.FindControl("YTDLabel").Visible = False
    End If
    End Sub
    Phillip Vong, Oct 10, 2006
    #1
    1. Advertising

  2. Phillip Vong

    Gozirra Guest

    YTDtest is a Decimal variable. The returned value of FindControl is a
    control object. These are obviously not the same thing. I think what
    you are wanting to do is get the Text property of the control and
    assign it to the YTDtest variable.

    Try
    YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    Label).Text)

    And you may not need to use FindControl. If the control is declared in
    the code beind, you can access it directly.
    YTDtest = Decimal.Parse(YTDLabel.Text)
    Gozirra, Oct 10, 2006
    #2
    1. Advertising

  3. Phillip Vong

    Phillip Vong Guest

    Maybe I just don't understand. I tried your code this way and nothing
    happend. I don't think it's test to see if the value is > 0 because it's
    now a text format. Am I right about that? How do you make it test to see
    if the decimal value is greater than 1?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim YTDtest As Decimal

    YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    Label).Text)

    If YTDtest > 0 Then

    FormView1.FindControl("YTDLabel").Visible = False

    End If

    End Sub






    "Gozirra" <> wrote in message
    news:...
    > YTDtest is a Decimal variable. The returned value of FindControl is a
    > control object. These are obviously not the same thing. I think what
    > you are wanting to do is get the Text property of the control and
    > assign it to the YTDtest variable.
    >
    > Try
    > YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    > Label).Text)
    >
    > And you may not need to use FindControl. If the control is declared in
    > the code beind, you can access it directly.
    > YTDtest = Decimal.Parse(YTDLabel.Text)
    >
    Phillip Vong, Oct 10, 2006
    #3
  4. Phillip Vong

    Gozirra Guest

    The type is a Decimal. Decimal.Parse converts the value of the labels
    text property to a decimal type and assigns it to your variable. I
    have tested a very simple example of this to make sure it works and
    have had no problems. Can you step through the code and see what's
    going on? I wonder if the FormView is causing problems. My small
    sample does not use a FormView and works perfectly.

    Phillip Vong wrote:
    > Maybe I just don't understand. I tried your code this way and nothing
    > happend. I don't think it's test to see if the value is > 0 because it's
    > now a text format. Am I right about that? How do you make it test to see
    > if the decimal value is greater than 1?
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    >
    > Dim YTDtest As Decimal
    >
    > YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    > Label).Text)
    >
    > If YTDtest > 0 Then
    >
    > FormView1.FindControl("YTDLabel").Visible = False
    >
    > End If
    >
    > End Sub
    >
    >
    >
    >
    >
    >
    > "Gozirra" <> wrote in message
    > news:...
    > > YTDtest is a Decimal variable. The returned value of FindControl is a
    > > control object. These are obviously not the same thing. I think what
    > > you are wanting to do is get the Text property of the control and
    > > assign it to the YTDtest variable.
    > >
    > > Try
    > > YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    > > Label).Text)
    > >
    > > And you may not need to use FindControl. If the control is declared in
    > > the code beind, you can access it directly.
    > > YTDtest = Decimal.Parse(YTDLabel.Text)
    > >
    Gozirra, Oct 11, 2006
    #4
  5. Phillip Vong

    Phillip Vong Guest

    This is what someone else told me to use and it worked. Gozirra, thanks for
    your help!!!

    Dim YTDLabel As Label
    YTDLabel = DirectCast(FormView1.FindControl("YTDLabel"), Label)

    Dim YTDtest As Decimal
    YTDTest = Decimal.Parse(YTDLabel.Text)







    "Gozirra" <> wrote in message
    news:...
    > The type is a Decimal. Decimal.Parse converts the value of the labels
    > text property to a decimal type and assigns it to your variable. I
    > have tested a very simple example of this to make sure it works and
    > have had no problems. Can you step through the code and see what's
    > going on? I wonder if the FormView is causing problems. My small
    > sample does not use a FormView and works perfectly.
    >
    > Phillip Vong wrote:
    >> Maybe I just don't understand. I tried your code this way and nothing
    >> happend. I don't think it's test to see if the value is > 0 because it's
    >> now a text format. Am I right about that? How do you make it test to
    >> see
    >> if the decimal value is greater than 1?
    >>
    >> Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    >> System.EventArgs)
    >>
    >> Dim YTDtest As Decimal
    >>
    >> YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    >> Label).Text)
    >>
    >> If YTDtest > 0 Then
    >>
    >> FormView1.FindControl("YTDLabel").Visible = False
    >>
    >> End If
    >>
    >> End Sub
    >>
    >>
    >>
    >>
    >>
    >>
    >> "Gozirra" <> wrote in message
    >> news:...
    >> > YTDtest is a Decimal variable. The returned value of FindControl is a
    >> > control object. These are obviously not the same thing. I think what
    >> > you are wanting to do is get the Text property of the control and
    >> > assign it to the YTDtest variable.
    >> >
    >> > Try
    >> > YTDtest = Decimal.Parse(CType(FormView1.FindControl("YTDLabel"),
    >> > Label).Text)
    >> >
    >> > And you may not need to use FindControl. If the control is declared in
    >> > the code beind, you can access it directly.
    >> > YTDtest = Decimal.Parse(YTDLabel.Text)
    >> >

    >
    Phillip Vong, Oct 11, 2006
    #5
    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. Kerri

    Formatting Decimals

    Kerri, Aug 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    410
    Steve C. Orr, MCSD
    Aug 21, 2003
  2. Kevin Spencer

    Re: Formatting Decimals

    Kevin Spencer, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Kevin Spencer
    Aug 26, 2003
  3. Brent Burkart

    asp.net text box for decimals

    Brent Burkart, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    487
    Chris Jackson
    Nov 12, 2003
  4. vivekian
    Replies:
    7
    Views:
    311
    vivekian
    Nov 25, 2005
  5. Louis
    Replies:
    7
    Views:
    771
    Michael Tsang
    Oct 1, 2009
Loading...

Share This Page