Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 27th, 2007, 03:40 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default Use variable inside a repeater?

Hi There,

Can anyone help me out with a problem I'm having and can't seem to find the answer to on trusty Google?

I have a variable in my codebehind which contains an integer value. I need to retrive this variable in my page code to perform a calculation inside an ASP:Repeater.

Code looks like this...

Code:
<%# Math.Round(DataBinder.Eval(Container.DataItem, "iVotes") / intVariable * 100, 0) %>
Okay, so basically I'm retreiving the number of votes from the database, then I want to divide that by the total number of votes, and times by 100 to get the percentage. I then use Math.Round to round off so there are no decimal places.

When I replace intVariable with a hard coded number it works fine. But when I try to use the variable it falls over.

I'm guessing there has to be a correct way of doing this.

Thanks!
 
Old July 28th, 2007, 05:13 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Can you define "falls over"? Do you get an error?

maybe you need to convert the variable to a different data type (Decimal for example) first to avoid rounding issues.

Cheers,

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 28th, 2007, 05:32 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar

Okay, the error I'm getting is "Compiler Error Message: BC30451: Name 'intVoteCount' is not declared."

Thing is, I am declaring it. This is what I have in my codebehind:
Code:
Dim rsVoteCount As SqlDataReader
rsVoteCount = SelectDB("SELECT SUM(iVotes) AS iVoteTotal FROM tblVotes WHERE iQuestionID =" & rsVote("iQuestionID"))
rsVoteCount.Read()
Dim intVoteCount As Integer = rsVoteCount("iVoteTotal")
Then in my inline page code I have:
Code:
<%# Math.Round((DataBinder.Eval(Container.DataItem, "iVotes") / intVoteCount) * 100, 0) %>
Any ideas? Any help you could offer would be greatly appreciated.
 
Old July 28th, 2007, 05:38 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You should probably make the intCount a public field or property on the class. That is, declare it outside any method, within the class like this:

Public IntCount As Integer = 0

Then in your code that accesses the database assign the value like this:

IntVoteCount = rsVoteCount("iVoteTotal")

This gives the IntCount field public scope so it's visible in the code behind.

You're probably better off doing all this in the code behind.

E.g.:

<%# CalculateStuff(DataBinder.Eval(Container.DataItem, "iVotes")) %>

Then make CalculateStuff a public method in the Code Behind that expects an object iVotes. Inside the method convert / cast that object to an Integer or other appropriate data type, and have CalculateStuff return a string value that you display in the page.

Hope this gives you some ideas.

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 28th, 2007, 05:57 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar

Thanks for your advice. I'm now no longer getting a compiler error from the server, although I'm not sure if I've implemented it correctly.

Now I have in my codebehind:
Code:
Public IntVoteCount As Integer = 1234

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

... Stuff here

End Sub
Was this how you intended for me to implement your suggestion?

Thanks!
 
Old July 28th, 2007, 06:02 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Not completely.

You have now hardcoded the value for IntVoteCount but I think you want it to come from the database, right?

So, just as in my example, declare it and set it to 0.

Then with the same code you were using earlier, assign it the value from rsVoteCount("iVoteTotal"), just as in my example.

Does this help?

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 28th, 2007, 06:20 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Sorry, yes I did do that. I don't think I explained myself correctly.

I just changed the "0" to "1234" to test something. But I've changed it back now.

This is the full code I now have.
Code:
Public IntVoteCount As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
Dim rsVoteCount As SqlDataReader
rsVoteCount = SelectDB("SELECT SUM(iVotes) AS iVoteTotal FROM tblVotes WHERE iQuestionID =" & rsVote("iQuestionID"))
rsVoteCount.Read()
intVoteCount = rsVoteCount("iVoteTotal")

End Sub
I'm just trying to write a public function now to perform all of the calculation, as you've suggested.

Thanks!
 
Old July 28th, 2007, 06:45 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
I'm just trying to write a public function now to perform all of the calculation, as you've suggested.
Okay, I've written a function which appears to work well.
Code:
Public Function CalculateVote(ByVal iVotes As Integer, iTotalVotes As Integer)
  Dim Calculate
  Calculate = Math.Round(iVotes / iTotalVotes * 100, 0)
  Return Calculate
End Function
There still appears to be a problem with the main code though. The code is still outputting "0" as the value of intVoteCount back to the page although I know the database is returning "343" on the line that has intVoteCount = rsVoteCount("iVoteTotal")
 
Old July 28th, 2007, 08:19 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

It's probably a timing issue. Maybe CalculateVote is called during data binding before you have your custom code that calculates intVoteCount?

Also, I don't see your method using intVoteCount anymore. Since it's declared in the code behind, you don't need to pass it from the markup into your custom function....

Imar


---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
 
Old July 28th, 2007, 08:39 AM
Authorized User
 
Join Date: Sep 2004
Location: Sydney, NSW, Australia.
Posts: 67
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar

You're exactly right! It was a timing issue. It's working perfectly now. Thank you so much for your help!

I don't suppose you know how to make Math.Round round up or down to the nearest whole number do you? I'm formatting my percentages to display without any decimal places and my current poll results in a total that only adds up to 99%.

It's strange because I'm actually re-writing this application from classic ASP and there I'm just using the Round() function which rounds up or down to the nearest whole number perfectly.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem accessing global variable inside functions jedijr88 Beginning PHP 3 June 29th, 2007 12:51 PM
ASP variable inside my JavaScript crmpicco Javascript How-To 1 June 4th, 2005 12:17 AM
set ASP variable values inside Javascript crmpicco HTML Code Clinic 1 March 9th, 2005 12:32 PM
rephrased question - for variable inside HTML <td> crmpicco Classic ASP Basics 1 February 11th, 2005 04:03 AM
use a JS-variable/function inside a tag cybermaarten Javascript 3 January 20th, 2005 05:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.