5 Replies - 2111 Views - Last Post: 17 June 2008 - 11:49 AM Rate Topic: -----

#1 Randall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-March 08

calculating a sum:

Post icon  Posted 16 June 2008 - 05:55 AM

Hi, I have a form like page for my website where the user can select a a certen amount of products and they click total and it totals whatever they have selected, it just dosnt seem to work?

Here is my code:

<%@ Page Language="VB" MaintainScrollPositionOnPostback="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

	Dim ttlPrice As Single


	Protected Sub GridView1_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
		Dim sngID, sngPrice As Single
		Dim txtDescription, txtList As String				

		sngID = GridView1.SelectedRow.Cells(1).Text()
		sngPrice = GridView1.SelectedRow.Cells(2).Text()
		txtDescription = Gridview1.SelectedRow.Cells(3).Text()
		
		ttlPrice = sngPrice + ttlPrice
		
		txtTotalPrice.Text = ttlPrice
		
		txtList = sngID & " " & txtDescription & " " & sngPrice
		txtMessage.Text = txtMessage.Text & vbCrLf & txtList
	End Sub




I hope you guys can help im real stuck with this!

Randall.

Is This A Good Question/Topic? 0
  • +

Replies To: calculating a sum:

#2 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 680
  • Joined: 26-May 08

Re: calculating a sum:

Posted 16 June 2008 - 06:20 AM

ttlPrice = sngPrice + ttlPrice
		
txtTotalPrice.Text = ttlPrice



That won't work. ttlPrice is not a global variable or atleast not static . correct me if I'm wrong I don't do vb.net but IMO your code is wrong.

In my opinion what it will do is add the values with total everytime you click the button. It's like a running total.

This post has been edited by mensahero: 16 June 2008 - 06:23 AM

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: calculating a sum:

Posted 16 June 2008 - 09:13 AM

Well one problem you are going to have is that you need to convert the values from the GridView to the correct data types in order to work with them mathematically. Otherwise you are just going to end up concatenating the values into one long string.
sngID = Convert.ToSingle(GridView1.SelectedRow.Cells(1).Text())
sngPrice = Convert.ToSingle(GridView1.SelectedRow.Cells(2).Text())


Give that a try and see if it gives you the results you are expecting. Otherwise provide an example of what it is not doing correctly and an example showing what you expect it should look like.
Was This Post Helpful? 0
  • +
  • -

#4 Randall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-March 08

Re: calculating a sum:

Posted 16 June 2008 - 10:34 PM

i tried your code but it just gave a error

would someone be able to help by going over it and show me what i need to do to make it work?

here is my whole code
<%@ Page Language="VB" MaintainScrollPositionOnPostback="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">


	Protected Sub GridView1_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
		Dim sngID, sngPrice As Single
		Dim txtDescription, txtList As String

		txtTotalPrice.Text = Format(GridView1.SelectedRow.Cells(3).Text + CInt(txtTotalPrice.Text), "Currency")

		sngID = (GridView1.SelectedRow.Cells(1).Text())
		sngPrice = (GridView1.SelectedRow.Cells(2).Text())
		txtDescription = (GridView1.SelectedRow.Cells(3).Text())

		txtList = sngID & " " & txtDescription & " " & sngPrice
		txtMessage.Text = txtMessage.Text & vbCrLf & txtList
	End Sub
		

	
	Protected Sub btnList_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		txtTotalPrice.Text = "0"
		txtMessage.Text = ""
	End Sub
		
	Protected Sub btnSubmitOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		'Note that you need to write the order and customer details here
	End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
	<title>Select events</title>
</head>

<body bgcolor="#eeeeee">
	<form id="form1" runat="server">
	<div>
	&nbsp;&nbsp;&nbsp; &nbsp;
	<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
	DataKeyNames="ID" DataSourceID="AccessDataSource1"
	onselectedIndexChanged="GridView1_SelectedIndexChanged1"
	Width="768px" AllowSorting="True" PageSize="4">
	<Columns>
	<asp:CommandField ShowselectButton="True" />
	<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="Description" />
	<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" DataFormatString="{0:C}"
	ApplyFormatInEditMode="True" HtmlEncode="False" />
	<asp:BoundField DataField="Tour" HeaderText="Tour" SortExpression="Tour" />
	<asp:ImageField DataImageUrlField="Image" HeaderText="Image">
	</asp:ImageField>
	</Columns>
	</asp:GridView>
	<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/database/db2.mdb"
	DeleteCommand="DELETE FROM [events] WHERE [ID] = ?" InsertCommand="INSERT INTO [events] ([ID], [Description], [Price], [Tour], [Image]) VALUES (?, ?, ?, ?, ?,)"
	SelectCommand="SELECT * FROM [events]" UpdateCommand="UPDATE [events] SET [Description] = ?, [Price] = ?, [Tour] = ?, [Image] = ? WHERE [ID] = ?">
	<DeleteParameters>
	<asp:Parameter Name="ID" Type="String" />
	</DeleteParameters>
	<UpdateParameters>
	<asp:Parameter Name="Description" Type="String" />
	<asp:Parameter Name="Price" Type="Decimal" />
	<asp:Parameter Name="Tour" Type="String" />
	<asp:Parameter Name="Image" Type="String" />
	<asp:Parameter Name="ID" Type="string" />
	</UpdateParameters>
	<InsertParameters>
	<asp:Parameter Name="ID" Type="String" />
	<asp:Parameter Name="Description" Type="String" />
	<asp:Parameter Name="Price" Type="Decimal" />
	<asp:Parameter Name="Tour" Type="String" />
	<asp:Parameter Name="Image" Type="String" />
	</InsertParameters>
	</asp:AccessDataSource>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
	&nbsp;<asp:Label ID="Label2" runat="server" Text="Orders"></asp:Label>
	<asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
	&nbsp;&nbsp;<asp:Button ID="btnList" runat="server" onclick="btnList_Click" Text="Clear Orders" /><br />
	&nbsp;&nbsp;<br />
	<br />
	&nbsp;<asp:Label ID="Label1" runat="server" Text="Total Price"></asp:Label>
	&nbsp;&nbsp;
	<asp:TextBox ID="txtTotalPrice" runat="server" Text="Total Price"></asp:TextBox>
	<br />
	<asp:Button ID="btnSubmitOrder" runat="server" onclick="btnSubmitOrder_Click" Text="SubmitOrder" />
	&nbsp;&nbsp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	&nbsp;&nbsp;
  
	</div>
	</form>
</body>
</html>




hope you guys can help me!

cheers

Randall
Was This Post Helpful? 0
  • +
  • -

#5 motcom  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,371
  • Joined: 16-June 08

Re: calculating a sum:

Posted 17 June 2008 - 04:18 AM

Hi Randall,

Have you used session variables? on each postbak, the items are cleared...
I don't have your database structure but this is what i think should do it
Just a note, when you added the totalprice to the textbox, i saw you added as string to a number, this will not work.

	Protected Sub GridView1_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
		Dim sngID, sngPrice As Single
		Dim txtDescription, txtList As String
		txtTotalPrice.Text = Session.Item("TotalPrice")
		txtTotalPrice.Text = Format(CDbl(txtTotalPrice.Text) + CDbl(Session.Item("TotalPrice")), "Currency")

		sngID = (GridView1.SelectedRow.Cells(1).Text())
		sngPrice = (GridView1.SelectedRow.Cells(2).Text())
		txtDescription = (GridView1.SelectedRow.Cells(3).Text())

		txtList = sngID & " " & txtDescription & " " & sngPrice
		txtMessage.Text = txtMessage.Text & vbCrLf & txtList
		Session.Add("TotalPrice", txtTotalPrice.Text)
	End Sub

	Protected Sub btnList_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		txtTotalPrice.Text = "0"
		txtMessage.Text = ""
	End Sub

	Protected Sub btnSubmitOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs)
		'Note that you need to write the order and customer details here
	End Sub

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Session.Add("TotalPrice", 0)
	End Sub



Hope this helps
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: calculating a sum:

Posted 17 June 2008 - 11:49 AM

View PostRandall, on 16 Jun, 2008 - 10:34 PM, said:

i tried your code but it just gave a error

Post your error messages.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1