0

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
CDspace
  • 2,639
  • 18
  • 30
  • 36
CentJohn
  • 1
  • 2
  • 2
    "What did i do wrong" Is this a riddle? Please tell us what error you got and where – Tim Schmelter Dec 01 '16 at 12:24
  • If `OrderDate` is a `datetime`(what it should be), dont pass a string – Tim Schmelter Dec 01 '16 at 12:26
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason Dec 01 '16 at 13:21

1 Answers1

0

Since this question is asked so many times here and there is a general solution to all of then is here.

Okay, since you mentioned that you are getting error here

command.Parameters.AddWithValue("@Prod_Info_id", CType(item.FindControl("Prod_Info_id"), Label).Text)

So I think you don't have label with id Prod_Info_id in that item.

Put the debugger there and debug that to find the reasion.

Or you can post you mark up so we can inspect that.

Community
  • 1
  • 1
Imad
  • 7,126
  • 12
  • 55
  • 112