Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 12th, 2006, 05:19 PM
Authorized User
 
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing Value Issue

Hi,

I wondered if someone could help with another issue I have with my code asp.net 1.1. and vb2003.

The code below relates to a shop page which allows a customer to add an item or items to a cart

and then go to a checkout page. However when you go to the checkout page the sale price of the

item selected is not being displayed. In fact it just displays a zero !!!

I've created a "GetPrice" function to try and get the value but this does not work ! Any ideas

how to resolve this issue ? Many thanks and here's the code :

<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    'Define datatable
    Dim objDT As System.Data.DataTable

    'Function to create random number
     Function RandomNo()
            If Session("CartID") Is Nothing Then

                'Create random number
                Dim RandomNumber As Integer
                RandomNumber = int((1+99999 - 10001) * rnd) + 10001
                'Display random number - testing only
                lblRandom.Text += CStr("Value is:" & RandomNumber)
                lblRandom.visible=False

                'Create date parameter
                Dim dateNow as Date = Date.Now
                Dim dtcart = dateNow.ToString("dd/MM/yyyy")
                'Remove brackets from date
                Dim datenum = (Replace(dtcart,"/",""))
                'Response.Write("datenum")


                'Set session equal to random number and date
                Session("CartID") = RandomNumber & datenum
                'Response.Write(Session("CartID"))
                'Call Createcart Function
                createCart()
            End If
    End Function



    Sub Page_Load()

            If Page.IsPostBack Then

                'Call random number function
                RandomNo()

                Dim intTypeID As Integer

                'Call GetItems Function
                GetItems(intTypeID)

              Select Case(Product.SelectedItem.Value)
                Case"GuitarCables"
                 DataList.DataSource = GetItems(8)
                 DataList.DataBind()
              End Select
              Select Case(Product.SelectedItem.Value)
                Case"Plectrums"
                 DataList.DataSource = GetItems(9)
                 DataList.DataBind()
               End Select
               Select Case(Product.SelectedItem.Value)
                Case"GuitarCases"
                  DataList.DataSource = GetItems(10)
                  DataList.DataBind()
               End Select
               Select Case(Product.SelectedItem.Value)
                Case"GuitarStraps"
                 DataList.DataSource = GetItems(11)
                 DataList.DataBind()
               End Select
               Select Case(Product.SelectedItem.Value)
                Case"GuitarTuners"
                 DataList.DataSource = GetItems(12)
                 DataList.DataBind()
               End Select
               Select Case(Product.SelectedItem.Value)
                Case"GuitarStrings"
                 DataList.DataSource = GetItems(13)
                 DataList.DataBind()
               End Select
               Select Case(Product.SelectedItem.Value)
                Case"GuitarStands"
                 DataList.DataSource = GetItems(14)
                 DataList.DataBind()
               End Select
             End If
         End Sub



        'Function to get required items
        Function GetItems(ByVal intTypeID As Integer) As System.Data.DataSet

            Dim strConnString As String =

ConfigurationSettings.AppSettings.Get("ConnectionS tring")
            strConnString = String.Format(strConnString, Server.MapPath("\db\nwguitars.mdb"))
            Dim dbConnection As System.Data.IDbConnection = New

System.Data.OleDb.OleDbConnection(strConnString)

            Dim queryString As String = "SELECT tblMake.strMake, tblProducts.strProductName,

tblColour.strColour, "& _
            "tblProducts.intQuantityInStock, tblProducts.curSalePrice, tblType.intTypeID, * "& _
            "FROM tblType INNER JOIN (tblMake INNER JOIN (tblColour INNER JOIN tblProducts ON "&

_
            "tblColour.intColourID=tblProducts.intColourID ) ON

tblMake.intMakeID=tblProducts.intMakeID) "& _
            "ON tblType.intTypeID=tblProducts.intTypeID WHERE ([tblProducts].[intTypeID] =

@intTypeID)"
            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection

            Dim dbParam_intTypeID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_intTypeID.ParameterName = "@intTypeID"
            dbParam_intTypeID.Value = intTypeID
            dbParam_intTypeID.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_intTypeID)

            Dim dataAdapter As System.Data.IDbDataAdapter = New

System.Data.OleDb.OleDbDataAdapter
            dataAdapter.SelectCommand = dbCommand
            Dim dataSet As System.Data.DataSet = New System.Data.DataSet
            dataAdapter.Fill(dataSet)

            Return dataSet
        End Function



         Function createCart()

            'Define cart

            objDT = New System.Data.DataTable("Cart")

            Dim CustomerID As Double

            'Set date and time
            Dim dtNow as Date = Date.Now
            Dim ts as String
            ts = dtNow.ToString("dd/MM/yyyy" & " " & "HH:mm:ss")


            'Updates Cart table in db
            UpdatetblCart(Session("CartID"),CustomerID,ts)

        End Function


         'Add To Cart Sub procedure
         Sub AddToCart(sender As Object, e As EventArgs)

            Dim CartID As Double

            'Passes ProductID as sender
            Dim intProductID As Integer = sender.CommandArgument'<%#

DataBinder.Eval(Container.DataItem, "intProductID")%>'

            Dim Price As Integer
            GetPrice(intProductID)

            Dim Quantity As Integer
            Quantity = 1

            'Updates CartItems table in db
            UpdatetblCartItems(Session("CartID"),Quantity,intP roductID,Price)


        End Sub



        Function GetPrice(ByVal intProductID As Integer) As System.Data.IDataReader
            Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB

Services=-4; Data Source=F:\Richy\NITLC\"& _
            "Stage5\ISPD\website\db\nwguitars.mdb"
            Dim dbConnection As System.Data.IDbConnection = New

System.Data.OleDb.OleDbConnection(connectionString )

            Dim queryString As String = "SELECT [tblProducts].[curSalePrice] FROM [tblProducts]

WHERE ([tblProducts].[intP"& _
            "roductID] = @intProductID)"
            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection

            Dim dbParam_intProductID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_intProductID.ParameterName = "@intProductID"
            dbParam_intProductID.Value = intProductID
            dbParam_intProductID.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_intProductID)

            dbConnection.Open
            Dim dataReader As System.Data.IDataReader =

dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

            Return dataReader
        End Function


        'Function to update Cart table in database
        Function UpdatetblCart(ByVal CartID As Double, ByVal CustomerID As Double, ByVal ts As

Date) As Integer
            Dim strConnString As String =

ConfigurationSettings.AppSettings.Get("ConnectionS tring")
            strConnString = String.Format(strConnString, Server.MapPath("\db\nwguitars.mdb"))
            Dim dbConnection As System.Data.IDbConnection = New

System.Data.OleDb.OleDbConnection(strConnString)

            Dim queryString As String = "INSERT INTO [tblCart] ([intCartID] , [intCustomerID],

[dtmDate]) "& _
            "VALUES (@CartID, @CustomerID, @DateTime)"

            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection

            Dim dbParam_CartID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_CartID.ParameterName = "@CartID"
            dbParam_CartID.Value = CartID
            dbParam_CartID.DbType = System.Data.DbType.Double
            dbCommand.Parameters.Add(dbParam_CartID)

            Dim dbParam_CustomerID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_CustomerID.ParameterName = "@CustomerID"
            dbParam_CustomerID.Value = CustomerID
            dbParam_CustomerID.DbType = System.Data.DbType.Double
            dbCommand.Parameters.Add(dbParam_CustomerID)

            Dim dbParam_ts As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_ts.ParameterName = "@DateTime"
            dbParam_ts.Value = ts
            dbParam_ts.DbType = System.Data.DbType.DateTime
            dbCommand.Parameters.Add(dbParam_ts)

            Dim rowsAffected As Integer = 0
            dbConnection.Open
            Try
                rowsAffected = dbCommand.ExecuteNonQuery
            Finally
                dbConnection.Close
            End Try

            Return rowsAffected
        End Function


        'Function to update CartItems table in database
        Function UpdatetblCartItems(ByVal CartID As Double, ByVal Quantity As Integer, ByVal

intProductID As Integer, ByVal Price As Integer) As Integer
            Dim strConnString As String =

ConfigurationSettings.AppSettings.Get("ConnectionS tring")
            strConnString = String.Format(strConnString, Server.MapPath("\db\nwguitars.mdb"))
            Dim dbConnection As System.Data.IDbConnection = New

System.Data.OleDb.OleDbConnection(strConnString)

            Dim queryString As String = "INSERT INTO [tblCartItems] ([intCartID]

,[intQuantityOrder], [intProductID], [curSalePrice]) "& _
            "VALUES (@CartID, @Quantity, @intProductID, @Price)"

            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection

            Dim dbParam_CartID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_CartID.ParameterName = "@CartID"
            dbParam_CartID.Value = CartID
            dbParam_CartID.DbType = System.Data.DbType.Double
            dbCommand.Parameters.Add(dbParam_CartID)

            Dim dbParam_Quantity As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_Quantity.ParameterName = "@Quantity"
            dbParam_Quantity.Value = Quantity
            dbParam_Quantity.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_Quantity)

            Dim dbParam_intProductID As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_intProductID.ParameterName = "@intProductID"
            dbParam_intProductID.Value = intProductID
            dbParam_intProductID.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_intProductID)

            Dim dbParam_Price As System.Data.IDataParameter = New

System.Data.OleDb.OleDbParameter
            dbParam_Price.ParameterName = "@Price"
            dbParam_Price.Value = Price
            dbParam_Price.DbType = System.Data.DbType.Int32
            dbCommand.Parameters.Add(dbParam_Price)

            Dim rowsAffected As Integer = 0
            dbConnection.Open
            Try
                rowsAffected = dbCommand.ExecuteNonQuery
            Finally
                dbConnection.Close
            End Try

            Return rowsAffected
        End Function


        'Checkout button
        Sub Checkout(sender As Object, e As EventArgs)
            Response.Redirect("cartv2.aspx")
        End Sub


        'Empty cart button
        Sub EmptyCart(sender As Object, e As EventArgs)

        End Sub

</script>
<html>
<head>
    <title></title>

    </script>
</head>
<body>
    <div align="center">
        <br />
        Select&nbsp;a product type and click on the <b>Search</b> button to see our current
        stock :
    </div>
    <form runat="server">
        <p align="center">
            <asp:DropDownList id="Product" runat="server">
                <asp:ListItem Value="GuitarCables">Guitar Cables</asp:ListItem>
                <asp:ListItem Value="Plectrums">Plectrums</asp:ListItem>
                <asp:ListItem Value="GuitarCases">Guitar Cases</asp:ListItem>
                <asp:ListItem Value="GuitarStraps">Guitar Straps</asp:ListItem>
                <asp:ListItem Value="GuitarTuners">Guitar Tuners</asp:ListItem>
                <asp:ListItem Value="GuitarStrings">Guitar Strings</asp:ListItem>
                <asp:ListItem Value="GuitarStands">Guitar Stands</asp:ListItem>
            </asp:DropDownList>
            <br />
            &nbsp;
        </p>
        <p align="center">
            <asp:Button id="btnSubmit" runat="server" Text="Search"></asp:Button>

        <div align="center">
            <asp:datalist id="DataList" runat="server" RepeatColumns="2"

RepeatDirection="Horizontal">
                <ItemTemplate>
                    <table cellpadding="5" align="left" border="1">
                        <tr>
                            <td valign="top" width="180" height="140">
                                Product ID: <%#

DataBinder.Eval(Container.DataItem, "intProductID") %>
                                <br />
                                <br />
                                Manufacturer: <%#

DataBinder.Eval(Container.DataItem, "strMake") %>
                                <br />
                                <br />
                                Product Name: <%#

DataBinder.Eval(Container.DataItem, "strProductName") %>
                                <br />
                                <br />
                                Colour: <%#

DataBinder.Eval(Container.DataItem, "strColour") %>
                                <br />
                                <br />
                                Quantity In Stock: <%#

DataBinder.Eval(Container.DataItem, "IntQuantityInStock") %>
                                <br />
                                <br />
                                Price: <%#

DataBinder.Eval(Container.DataItem, "curSalePrice", "{0:c}") %>
                            </td>
                            <td align="center" width="110">
                                <img src='./images/<%# Databinder.Eval(Container.DataItem,

"strImage") %>' valign="top" />
                            </td>
                            <td>
                                <asp:Button id="btnBuy" runat="server" Text="Buy"

onclick="AddToCart" CommandArgument='<%# DataBinder.Eval(Container.DataItem,

"intProductID")%>'></asp:Button>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:datalist>
        </div>
    </form>

</body>
</html>





 
Old December 14th, 2006, 03:52 AM
Friend of Wrox
 
Join Date: May 2005
Posts: 227
Thanks: 1
Thanked 7 Times in 7 Posts
Default

rsm42:
It is imperative that you review the difference between FUNCTIONs and SUBROUTINEs. One important difference is Functions MUST return An Answer. In your source code you call the RandomNumber Generator but you do not return the number generated. There are other instances, where u call the function and in the function you return an integer or dataset, but these objects are not set or returned in the calling or invoking statements.

Hope this helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Checkout Issue rsm42 ASP.NET 1.0 and 1.1 Basics 4 November 27th, 2006 03:50 PM
masterpage issue rsearing ASP.NET 2.0 Basics 1 November 15th, 2006 10:11 PM
Simple 'passing variables between forms' issue... overture C# 4 October 14th, 2005 02:30 PM
Another issue islandtiu BOOK: Beginning ASP 3.0 1 February 14th, 2005 11:49 AM
Recordset Issue srpathak Access VBA 5 December 3rd, 2003 09:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.