2016-12-01 77 views
0

我使用VB.Net進行開發。我正在試圖建立一個購物車。在這裏,我試圖將中繼器購物車插入到購物歷史記錄表中將中繼器數據插入數據庫時​​出錯

這是我得到的錯誤。

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)

這裏是標記

<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 
+2

「我做錯了什麼」這是一個謎語嗎?請告訴我們你得到了什麼錯誤,並在哪裏 –

+0

如果'OrderDate'是'datetime'(它應該是什麼),不要傳遞字符串 –

+0

[什麼是NullReferenceException,我該如何解決它?](http ://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason

回答

0

代碼,因爲這個問題被問了很多次在這裏和那裏是一個通用的解決方案給所有的則是here

好吧,既然你提到你在這裏得到錯誤

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

所以我認爲你沒有標籤與該項目ID Prod_Info_id

把調試器放在那裏,並調試,以找到reasion。

或者你可以張貼你的標記,以便我們可以檢查。

+1

如果這個問題被反覆詢問,最好的辦法是*標記它作爲副本*。 –

相關問題