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 29th, 2006, 12:20 PM
Authorized User
 
Join Date: Sep 2006
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default Quantity Error

Hi,

I wondered if someone can help ? Within an e-commerce website written in asp.net

1.1 with vb2003 and using an access database, I want to create an if statement to

check that there are sufficient items in stock within the database before allowing

a customer to add the item into the cart. otherwise to display a message saying

item is out of stock.

I have created a function to look up the selected value chosen to check how many

of these items are in stock :

Code:
 Function CheckQuantity(ByVal intProductID As Integer) As System.Data.IDataReader
        Dim strConnString As String = 

ConfigurationSettings.AppSettings.Get("ConnectionString")
        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 [tblProducts].[intProductID], 

[tblProducts].[intQuantityInStock] FROM [tbl"& _
        "Products] WHERE ([tblProducts].[intProductID] = @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.CommandBehavior.CloseConnection)

        Return dataReader
    End Function
Within my AddToCart sub procedure I then want to use an if statement such as

below. However when I reference the intQuantityInStock value in the line

"lblQuantity.Text = IntQuantityInStock" the label just displays a zero. I've

tested the previous function which works fine. How do I overcome this ?

Code:
'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")%>'

        'Set quantity value
        Dim Quantity As Integer
        Quantity = 1

        Dim intQuantityInStock As Integer

        'Check enough quantity in stock before adding to cart
        CheckQuantity(intProductID)

        dgList.DataSource = CheckQuantity(intProductID)
        dgList.DataBind()

        lblQuantity.Text = IntQuantityInStock
        lblQuantity.Visible = True

        If Quantity < intQuantityInStock
            lblError.Text = "This item is currently low in stock. Please select an 

alternative"
            lblError.Visible = True
        End If

......

 
Old December 31st, 2006, 01:25 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

You are trying to do 2 different things here. You are trying to determine how many products are in the database for a specific item and then you are returning a Datareader which that is all fine well and good, but the only thing you do with the datareader is bind it to a datalist.

The reason IntQuantityInStock is 0 is because you only ever declare it, you never set a value to it. this is not how I would do this but, for simplicity sake, i will show you how you can do it without modifying to much code:

Public function CheckQuantity(byval intProductID as integer) as boolean
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 [tblProducts].[intProductID], [tblProducts].[intQuantityInStock] FROM [tbl"& _
        "Products] WHERE ([tblProducts].[intProductID] = @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)

        While datareader.read
             Dim iQuantity as integer = Datareader("intProductID")
        end while

        If iQuantity > 1 then
          Return True
        Else
          Return False
        end if
end function

Your method then becomes this:

     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")%>'

        If Not CheckQuantity(intProductID) then
            lblError.Text = "This item is currently low in stock. Please select an alternative"
            lblError.Visible = True
        Else
            lblQuantity.Text = IntQuantityInStock
            lblQuantity.Visible = True

        '//these 2 lines will no longer work
        'dgList.DataSource = CheckQuantity(intProductID)
        'dgList.DataBind()
        End If
end sub

this obviously impacts your progrm flow so you will need to work with that. but, like i said, you were never setting the value of your integer to anything which is why it was returing 0.

-------------------------
I will only tell you how to do it, not do it for you.
Unless, of course, you want to hire me to do work for you.

^^Thats my signature





Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm to Score Global Quality-Quantity of File asgarcymed VBScript 0 November 24th, 2007 11:12 PM
Updating products quantity when order item insert Tawanda BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 1 June 7th, 2007 02:30 PM
Shopping Cart quantity update problem j2 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 August 21st, 2006 02:39 PM
Check quantity stock level Fehrer Access VBA 17 March 29th, 2004 06:11 PM





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