【发布时间】:2016-12-01 12:23:07
【问题描述】:
我使用 VB.Net 进行开发。我正在尝试构建一个购物车。在这里,我尝试将转发器购物车项目插入购物历史记录表中
这是我得到的错误。
对象引用未设置为对象的实例。
来源错误:
第 182 行:将命令作为新的 SqlCommand(queryString, connection)
第 183 行:
第 184 行:command.Parameters.AddWithValue("@Prod_Info_id", CType(item.FindControl("Prod_Info_id"), Label).Text)
这是标记
<asp:Repeater ID="ShoppingCartrepeater" runat="server">
<HeaderTemplate>
<table class="shopping-table">
<tr>
<th colspan="2">Product Image and Name</th>
<th>SKU</th>
<th>Cost</th>
<th>Quantity</th>
<%--<th>Service Charge</th>--%>
<th>Discount</th>
<th>Total</th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="image-column"><a href="#"> <asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Prod_Image_Front")%>'/></a>
</td>
<td><p><a href="#"><asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Product")%>' ></asp:Label></a></p></td>
<td><p><asp:Label ID="Label2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Prod_Info_id")%>'></asp:Label></p></td>
<td><p>₦<asp:Label ID="PCost" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Cost")%>' /></p></td>
<td>
<p> <asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Qty")%>'></asp:Label> </p>
<%--<asp:TextBox ID="txtQTY" runat="server" type="text" value="1"></asp:TextBox>--%>
<%--<input type="text" value="1">--%>
</td>
<%--<td><p><asp:Label ID="lblTax" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Service_Charge")%>'></asp:Label></p></td>--%>
<td><p><asp:Label ID="lblDiscount" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Discount")%>'></asp:Label></p></td>
<td><p>₦<asp:Label ID="lblTotal" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Total")%>'></asp:Label></p></td>
<td>
<p>
<a href='ShoppingCart.aspx?action=remove&id=<%# DataBinder.Eval(Container.DataItem, "Prod_Info_id") %>'><i class="icons icon-ok-3"></i> Delete</a><br>
<asp:Label ID="Label4" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Cust_Size")%>' Visible="False"></asp:Label>
<%--<a href="#" class="red-hover"><i class="icons icon-cancel-3"></i> Delete</a>--%>
</p>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
下面是我的代码
Sub CreateOrderHistory()
Dim Prod_Info_id As Label
Dim Qty As Label
Dim Total As Label
Dim Cost As Label
Dim Cust_Size As Label
Dim connectionString As [String] = ConfigurationManager.ConnectionStrings("BG").ConnectionString
Dim queryString As [String] = ""
Using connection As New SqlConnection(connectionString)
For Each item As RepeaterItem In ShoppingCartrepeater.Items
Prod_Info_id = CType(item.FindControl("Prod_Info_id"), Label)
Qty = CType(item.FindControl("Cost"), Label)
Total = CType(item.FindControl("Qty"), Label)
Cost = CType(item.FindControl("Total"), Label)
Cust_Size = CType(item.FindControl("Cust_Size"), Label)
queryString = "INSERT INTO BGOrderHIstory (Prod_Info_id,Shoper_id,BGOrder_id,OrderStatus,OrderDate,Total,Cost,Cust_Size) VALUES (@Prod_Info_id,@Shoper_id,@BGOrder_id,@OrderStatus,@OrderDate,@Total,@Cost,@Cust_Size)"
Dim command As New SqlCommand(queryString, connection)
command.Parameters.AddWithValue("@Prod_Info_id", CType(item.FindControl("Prod_Info_id"), Label).Text)
command.Parameters.AddWithValue("@Cost", CType(item.FindControl("Cost"), Label).Text)
command.Parameters.AddWithValue("@Shoper_id", Shoperid)
command.Parameters.AddWithValue("@BGOrder_id", Ihenchoro)
command.Parameters.AddWithValue("@OrderStatus", 0)
command.Parameters.AddWithValue("@OrderDate", Format(Now(), "yyyy-MM-dd HH:mm"))
'.InsertParameters.Add("Special_Request", txtSpecial.Text)
command.Parameters.AddWithValue("@Total", CType(item.FindControl("Total"), Label).Text)
'.InsertParameters.Add("GTotal", FinlTotal)
'.InsertParameters.Add("Service_Charge", 500)
'.InsertParameters.Add("Last_update", Date.UtcNow.ToString)
command.Parameters.AddWithValue("@Cust_Size", CType(item.FindControl("Cust_Size"), Label).Text)
command.ExecuteNonQuery()
Next
End Using
End Sub
【问题讨论】:
-
“我做错了什么” 这是个谜吗?请告诉我们您遇到了什么错误以及在哪里
-
如果
OrderDate是datetime(应该是什么),不要传递字符串