Retrieve Field Values into Variables

Discussion in 'ASP .Net' started by j.t.w@juno.com, Jul 14, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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>

    <> wrote in message
    news:...
    > 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
    >
     
    Ken Cox [Microsoft MVP], Jul 15, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page