473,394 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Retrieve Field Values into Variables

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

Jul 14 '06 #1
1 1810
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>

<j.***@juno.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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

Jul 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Roar | last post by:
Hi! I have got 1 access 2000 DB, one simple search form, and 3 .asp pages (one for deleting a record, one for inserting a record and one for listing searchresults). Deleting records works fine,...
8
by: Jack | last post by:
Hi, Here is my problem: I am logging in to a page, where the page retrieves a record from a database. The text boxes are used in the display formto let the users update the fields, if they...
6
by: Salvani Langosta | last post by:
In an Access 97 database, I use serveral global variables that hold information about the database, for example: gstrFileServer - holds the server root where the database is stored...
8
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: ...
9
by: Don | last post by:
Say I have a class like so: Public Class MyClass Public Prop1 as Integer Public Prop2 As Integer Public Prop3 As Integer End Class Is it possible to retrieve a list of the variables or...
6
by: zoro | last post by:
Hi, I am looking for the recommended way to retrieve several values from a single record, i.e. a typical lookup scenario. An example would be a query that needs to retrieve user_name, user_addres,...
1
by: glady | last post by:
Hi, I would like to have a php form that gets input from user and display data if they have already entered for that particular record in the same form. For example, if i have a form with 20...
5
by: Ken | last post by:
I'm trying to run a loop to capture column property information from a table in my datasource. Can anybody see where this is going wrong? Dim tbl As New DataTable Dim col As DataColumn Dim x...
9
by: Mel | last post by:
I have 10 columns total. 3 of them are invisible. The rest are read- only BoundFields, 3 of which are editable fields using TemplateFields. Upon editing, I want to validate what the user enters...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.