Creating a variable to accept Decimals.

P

Phillip Vong

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
 
G

Gozirra

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)
 
P

Phillip Vong

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
 
G

Gozirra

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.
 
P

Phillip Vong

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)
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top