Hi there
i got some troubles with the shopping cart and i could not find the solution , can you help me ? thanks
Here is my code Order.aspx
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:SqlDataSource ID="ProductCategoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT DISTINCT [ProductCategory] FROM [Products]">
</asp:SqlDataSource>
<asp:DropDownList ID="ProductCategoryDropDownList" runat="server"
DataSourceID="ProductCategoryDataSource" DataTextField="ProductCategory">
</asp:DropDownList>
<br />
<br />
<asp:PlaceHolder ID="productPlaceHolder" runat="server"></asp:PlaceHolder>
<asp:Button ID="OrderButton" runat="server" Text="Order" Width="95px" />
</asp:Content>
Order aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class Order
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim membershipuser As MembershipUser = Membership.GetUser()
If Not membershipuser Is Nothing Then
ViewState("username") = Membership.GetUser().ToString()
Else
Response.Redirect("Login.aspx")
End If
End If
Dim ProductName As String
Dim ProductID As String
Dim ProductPrice As String
Dim x As Integer
Dim productcodeArrayList As ArrayList = New ArrayList()
Dim productpriceArrayList As ArrayList = New ArrayList()
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim selectstatement As String = "SELECT * FROM Products WHERE ProductCategory=@ProductCategory"
Dim cmd As SqlCommand = New SqlCommand(selectstatement, conn)
cmd.Parameters.AddWithValue("@ProductCategory", ProductCategoryDropDownList.SelectedValue)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
ProductName = reader("ProductName").ToString().Trim()
ProductID = reader("ProductID").ToString().Trim()
ProductPrice = reader("ProductPrice").ToString().Trim()
productcodeArrayList.Add(ProductID)
productpriceArrayList.Add(ProductPrice)
productPlaceHolder.Controls.Add(New LiteralControl("(" & x & ") "))
Dim theLabel As Label = New Label()
theLabel.ID = "label" & x.ToString()
theLabel.Text = ProductName & " Qty: "
productPlaceHolder.Controls.Add(theLabel)
Dim theTextBox As TextBox = New TextBox()
theTextBox.ID = "textbox" & x.ToString()
theTextBox.Width = "20"
productPlaceHolder.Controls.Add(theTextBox)
productPlaceHolder.Controls.Add(New LiteralControl("<br />"))
x += 1
End While
conn.Close()
ViewState("productcodes") = productcodeArrayList
ViewState("productprices") = productpriceArrayList
End Sub
Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
Dim username = ViewState("username")
Dim productcodeArrayList As ArrayList = ViewState("productcodes")
Dim productpriceArrayList As ArrayList = ViewState("productprices")
Dim salesb As StringBuilder = New StringBuilder()
Dim theindex As Integer
Dim stringlength As Integer
Dim numbersize As Integer
Dim thequantity As String
Dim ctl As Control
For Each ctl In productPlaceHolder.Controls
If TypeOf ctl Is TextBox Then
thequantity = CType(ctl, TextBox).Text
If (thequantity.Length > 0) Then
stringlength = ctl.ID.ToString().Length
numbersize = stringlength - 7
theindex = Right$(ctl.ID.ToString(), numbersize)
salesb.Append("INSERT INTO Orders (ProductID, Quantity, OrderPrice, CustomerUserName) VALUES ('")
salesb.Append(productcodeArrayList(theindex) & "', ")
salesb.Append(thequantity & ", ")
salesb.Append(productpriceArrayList(theindex) & ",'")
salesb.Append(username & "') ")
End If
End If
Next
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim sqlCommand As String = "SET IDENTITY_INSERT Orders ON"
Dim cmd As SqlCommand = New SqlCommand(salesb.ToString(), conn)
cmd.ExecuteNonQuery()
For Each ctl In productPlaceHolder.Controls
If TypeOf ctl Is TextBox Then
CType(ctl, TextBox).Text = ""
End If
Next
conn.Close()
End Sub
End Class
Table Products
![Name: cart3_zps89642d84.jpg
Views: 2
Size: 24.5 KB]()
Table Orders
![Name: Cart5_zps838ce4cb.jpg
Views: 2
Size: 19.8 KB]()
The dropdownlist to select item from @ProductCategory
![Name: Cart1_zps0a109a9d.jpg
Views: 2
Size: 5.8 KB]()
And i got this error ???
ExecuteNonQuery: CommandText property has not been initialized
![Name: Cart2_zps489172fc.jpg
Views: 2
Size: 32.4 KB]()
i got some troubles with the shopping cart and i could not find the solution , can you help me ? thanks
Here is my code Order.aspx
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<asp:SqlDataSource ID="ProductCategoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT DISTINCT [ProductCategory] FROM [Products]">
</asp:SqlDataSource>
<asp:DropDownList ID="ProductCategoryDropDownList" runat="server"
DataSourceID="ProductCategoryDataSource" DataTextField="ProductCategory">
</asp:DropDownList>
<br />
<br />
<asp:PlaceHolder ID="productPlaceHolder" runat="server"></asp:PlaceHolder>
<asp:Button ID="OrderButton" runat="server" Text="Order" Width="95px" />
</asp:Content>
Order aspx.vb
Imports System.Data
Imports System.Data.SqlClient
Partial Class Order
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim membershipuser As MembershipUser = Membership.GetUser()
If Not membershipuser Is Nothing Then
ViewState("username") = Membership.GetUser().ToString()
Else
Response.Redirect("Login.aspx")
End If
End If
Dim ProductName As String
Dim ProductID As String
Dim ProductPrice As String
Dim x As Integer
Dim productcodeArrayList As ArrayList = New ArrayList()
Dim productpriceArrayList As ArrayList = New ArrayList()
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim selectstatement As String = "SELECT * FROM Products WHERE ProductCategory=@ProductCategory"
Dim cmd As SqlCommand = New SqlCommand(selectstatement, conn)
cmd.Parameters.AddWithValue("@ProductCategory", ProductCategoryDropDownList.SelectedValue)
Dim reader As SqlDataReader = cmd.ExecuteReader()
While reader.Read()
ProductName = reader("ProductName").ToString().Trim()
ProductID = reader("ProductID").ToString().Trim()
ProductPrice = reader("ProductPrice").ToString().Trim()
productcodeArrayList.Add(ProductID)
productpriceArrayList.Add(ProductPrice)
productPlaceHolder.Controls.Add(New LiteralControl("(" & x & ") "))
Dim theLabel As Label = New Label()
theLabel.ID = "label" & x.ToString()
theLabel.Text = ProductName & " Qty: "
productPlaceHolder.Controls.Add(theLabel)
Dim theTextBox As TextBox = New TextBox()
theTextBox.ID = "textbox" & x.ToString()
theTextBox.Width = "20"
productPlaceHolder.Controls.Add(theTextBox)
productPlaceHolder.Controls.Add(New LiteralControl("<br />"))
x += 1
End While
conn.Close()
ViewState("productcodes") = productcodeArrayList
ViewState("productprices") = productpriceArrayList
End Sub
Protected Sub OrderButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OrderButton.Click
Dim username = ViewState("username")
Dim productcodeArrayList As ArrayList = ViewState("productcodes")
Dim productpriceArrayList As ArrayList = ViewState("productprices")
Dim salesb As StringBuilder = New StringBuilder()
Dim theindex As Integer
Dim stringlength As Integer
Dim numbersize As Integer
Dim thequantity As String
Dim ctl As Control
For Each ctl In productPlaceHolder.Controls
If TypeOf ctl Is TextBox Then
thequantity = CType(ctl, TextBox).Text
If (thequantity.Length > 0) Then
stringlength = ctl.ID.ToString().Length
numbersize = stringlength - 7
theindex = Right$(ctl.ID.ToString(), numbersize)
salesb.Append("INSERT INTO Orders (ProductID, Quantity, OrderPrice, CustomerUserName) VALUES ('")
salesb.Append(productcodeArrayList(theindex) & "', ")
salesb.Append(thequantity & ", ")
salesb.Append(productpriceArrayList(theindex) & ",'")
salesb.Append(username & "') ")
End If
End If
Next
Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ToString())
conn.Open()
Dim sqlCommand As String = "SET IDENTITY_INSERT Orders ON"
Dim cmd As SqlCommand = New SqlCommand(salesb.ToString(), conn)
cmd.ExecuteNonQuery()
For Each ctl In productPlaceHolder.Controls
If TypeOf ctl Is TextBox Then
CType(ctl, TextBox).Text = ""
End If
Next
conn.Close()
End Sub
End Class
Table Products
Table Orders
The dropdownlist to select item from @ProductCategory
And i got this error ???
ExecuteNonQuery: CommandText property has not been initialized