Retrieve Field Values into Variables

J

j.t.w

Hi.

I'm new to ASP.Net and I'm running into a simple problem (I think).

I'm trying to get 2 values from 2 separate queries into 2 different
variables. I would then like to take the difference of the two
varaibles and place it into a label on an .aspx page.

Basically,
MyGrossSales = GrossSales from query
MyTotalCredit = TotalCredit from query
MyLabel = MyGrossSales - MyTotalCredit

I can retrieve data and place it into a repeater and/or other data
controls, but I've not found a way to get data into a variable(s) to be
used in code.

Can someone help me with this? Am I on the right track? Is there a
better (or simpler) way to accomplish this?

Here's what I have so far.

----------------- Start of Code ----------------
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
MyConnection = New SqlConnection("Server=MyServer;Integrated
Security=True;Database=MyDatabase")
MyCommand = New SqlDataAdapter("SELECT SUM(tot_dollars) AS
GrossSales FROM OEORDHDR_SQL WHERE (ord_type = 'I')", MyConnection)
DS = New DataSet
MyCommand.Fill(DS, "GrossSales")
Repeater1.DataSource = DS
Repeater1.DataBind()

Dim DS_2 As DataSet
Dim MyConnection_2 As SqlConnection
Dim MyCommand_2 As SqlDataAdapter
MyConnection_2 = New SqlConnection("Server=MyServer;Integrated
Security=True;Database=MyDatabase")
MyCommand_2 = New SqlDataAdapter("SELECT SUM(tot_dollars) AS
TotalCredits FROM OEORDHDR_SQL WHERE (ord_type = 'C')", MyConnection_2)
DS_2 = New DataSet
MyCommand_2.Fill(DS_2, "TotalCredits")
Repeater2.DataSource = DS_2
Repeater2.DataBind()

End Sub
----------------- End of Code ---------------

I've also read about getting data via something like
DS.Tables.Item(0).Rows(0)("GrossSales"), but I have not figured out how
to use it.

Thank you for your help,
j.t.w
 
K

Ken Cox [Microsoft MVP]

Hi JTW,

If understand what you're trying to accomplish, I suspect you can do it all
in one shot within the declarative markup. You can do the math as you
display the result on the table.

Here's an example of what I mean. There are other ways of doing this with a
helper function if this doesn't suit your needs.

Let us know?

Ken
Microsoft MVP [ASP.NET]


<%@ Page Language="VB" %>

<!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 Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
Repeater1.DataSource = CreateDataSource()
Repeater1.DataBind()
End Sub
Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("GrossSales", GetType(Decimal)))
dt.Columns.Add(New Data.DataColumn _
("TotalCredit", GetType(Decimal)))
Dim i As Integer
For i = 1 To 5
dr = dt.NewRow()
dr(0) = i + i * 800
dr(1) = i + i * 700
dt.Rows.Add(dr)
Next i
Return dt
End Function


</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Field and Variables</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:repeater id="Repeater1" runat="server">
<itemtemplate>
<asp:label id="MyGrossSales" runat="server" text='<%#
eval("GrossSales")%>'>
</asp:label><br />
<asp:label id="MyTotalCredit" runat="server" text='<%#
eval("TotalCredit")%>'>
</asp:label><br />
<asp:label id="MyLabel" runat="server" text='<%#
eval("GrossSales")-eval("TotalCredit")%>'>
</asp:label><br /><hr />
</itemtemplate>
</asp:repeater>

</div>
</form>
</body>
</html>
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top