Thursday, March 29, 2012

Passing Value Query

Hi,

I have an e-commerce website written in asp 1.1, vb.net and utilising an access database. Within the database I have a ProductID value to identify each product. Within the following sub procedure I want to be able to reference this value as I want to be able to check if there are enough items in stock. The code below uses the cartitemID and quantity as variables. However to reference individual items in the database I surely will need to pass the ProductID value. How should I approach this ?

[CODE]

Sub dgCart_Update(sender As Object, e As DataGridCommandEventArgs)

Dim intCartitemID As Integer
Dim txtQuantity As TextBox
Dim intQuantity As Integer

'Passes ProductID as sender

'Dim intProductID As Integer ??

intCartitemID = dgCart.DataKeys(e.Item.ItemIndex)
txtQuantity = e.Item.FindControl("txtQuantity")
intQuantity = txtQuantity.Text

'Define intQuantityInStock and reference to CheckQuantity function
Dim intQuantityInStock As Integer = CheckQuantity(intProductID)

'If statement to check enough quantity in stock
If intQuantityInStock < intQuantity
lblError.Text = "This item is currently low in stock. Please select an alternative</b>"
lblError.Visible = True
Else
Dim strSQL As String = "UPDATE [tblCartItems] SET [intQuantityOrder]=@dotnet.itags.org.Quantity WHERE "& _
"intCartitemID = @dotnet.itags.org.intCartitemID"

Dim dbCommand As New OleDbCommand(strSQL, dbConnection)

Dim cmd As New OleDbCommand

dbCommand.Parameters.Add("@dotnet.itags.org.Quantity", intQuantity)
dbCommand.Parameters.Add("@dotnet.itags.org.intCartitemID", intCartitemID)

dbConnection.Open()
dbCommand.ExecuteNonQuery()
dbConnection.Close()

dgCart.EditItemIndex = -1
dgCart.DataSource = DisplayCart()
dgCart.DataBind()

End If

End Sub
[/CODE]

You should be able to set this value in, say the CommandArgument of the button that is being clicked

ex:

<asp:Button
ID="btnAddToCart" runat="server"
CommandArgument='<%# Eval( "ProductID" ) %>'
CommandName="Product"
Text="Add To Cart" />

Then in your code behind, assign intProductID = (int) e.CommandArgument

Depending on what else you are doing with your grid, you may have to wrap an "if" around some code, checking if e.CommandName == "Product"


Thanks - unfortunately I'm not using a button in this instance. How else could I approach it ?


Fixed it thanks

0 comments:

Post a Comment