Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Retrieve Field Values into Variables

Reply
Thread Tools

Retrieve Field Values into Variables

 
 
j.t.w@juno.com
Guest
Posts: n/a
 
      07-14-2006
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

 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      07-15-2006
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>

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript validation for a not required field, field is onlyrequired if another field has a value jr Javascript 3 07-08-2010 10:33 AM
Populate Hidden field on post back and retrieve value from Hidden Field Rick ASP .Net 3 04-13-2010 05:38 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
1.Enter space bar for field names and save the field.The field shoud not get saved and an alert should be there as"Space bars are not allowed" Sound Javascript 2 09-28-2006 02:43 PM
comparing field data from previous field in loop with null values abbylee26@hotmail.com ASP General 6 02-15-2006 01:59 PM



Advertisments