I develop with VB.Net. I am trying to build a shopping cart. Here I am trying to insert the repeater cart items into Shopping history table
This is the error i got.
Object reference not set to an instance of an object.
Source Error:
Line 182: Dim command As New SqlCommand(queryString, connection)
Line 183:
Line 184: command.Parameters.AddWithValue("@Prod_Info_id", CType(item.FindControl("Prod_Info_id"), Label).Text)
Here is the Markup
<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>
Here is my Code behind
 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
 
     
     
    