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