I am working with a shopping cart (I am suing a book). At this point I
am trying to add the cartID, the productID, quantity and time. At the
same time it is supposed to add a cookie. When I click the add to cart
button it gives me a postback but does not enter data in the database
or add the cookie. I have tried to work with permissions. Since it is
on my local machine I have added permission to everything I can think
of. Here is the code. I would love some help.
One thing I have considered is that my example is using a button in a
datalist while I am in a datagrid. Can that be an issue?
The button is in a template column of the datagrid. Here is the code
behind.
Private Sub datagrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
datagrid1.ItemCommand
'in the command arguement of ther button that was clicked
'in the datagrid contains the productID
Dim intProductID As String = e.CommandArgument
'add the productto the shopping cart
ShoppingCart.AddProduct(intProductID)
End Sub
It is referring to a class called ShoppingCart.addproduct which is the
following
Public Shared Function AddProduct(ByVal intProductID As String)
' Create the connection object
Dim connection As New SqlConnection(connectionString)
' Create and initialize the command object
Dim command As New SqlCommand("AddProductToCart", connection)
command.CommandType = CommandType.StoredProcedure
' Add an input parameter and supply a value for it
command.Parameters.Add("@CartID", SqlDbType.Char, 36)
command.Parameters("@CartID").Value = shoppingCartID
' Add an input parameter and supply a value for it
command.Parameters.Add("@intProductID", SqlDbType.Int)
command.Parameters("@intProductID").Value = intProductID
' Open the connection, execute the command, and close the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
End Function
I am also trying to take a cookie at the same time. It is not working
at all.
Here is the cookie code.
Private Shared ReadOnly Property shoppingCartID()
Get
Dim context As HttpContext = HttpContext.Current
' If the JokePoint_CartID cookie doesn't exit
' on client machine we create it with a new GUID
If context.Request.Cookies("doughboys_cartID") Is Nothing
Then
' Generate a new GUID
Dim CartID As Guid = Guid.NewGuid()
' Create the cookie and set its value
Dim cookie As New HttpCookie("doughboys_cartID",
CartID.ToString)
' Current Date
Dim currentDate As DateTime = DateTime.Now()
' Set the time span to 10 days
Dim ts As New TimeSpan(10, 0, 0, 0)
' Expiration Date
Dim expirationDate As DateTime = currentDate.Add(ts)
' Set the Expiration Date to the cookie
cookie.Expires = expirationDate
' Set the cookie on client's browser
context.Response.Cookies.Add(cookie)
' return the Cart ID
Return CartID.ToString()
Else
' return the value stored in JokePoint_CartID
Return
context.Request.Cookies("doughboys_CartID").Value
End If
End Get
End Property