getting datarow vales in gridview error

7331

New Member
I have the following aspx code;\[code\] <asp:GridView ID="GridView1" runat="server" AllowPaging="True"AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductId"DataSourceID="edsinventory" OnRowCommand="GridView1_RowCommand"ShowFooter="true" CssClass="mGrid"> <Columns> <asp:TemplateField ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> <FooterTemplate><asp:LinkButton ID="LinkButton3" runat="server" CommandName="Insert">Insert</asp:LinkButton></FooterTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Id" InsertVisible="False" SortExpression="Id"> <EditItemTemplate> <asp:Label ID="lblEditId" runat="server" Text='<%# Eval("Id") %>'></asp:Label> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblId" runat="server" Text='<%# Bind("Id")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ProductId" SortExpression="ProductId"> <EditItemTemplate> <asp:TextBox ID="tbProdId" Width="50" runat="server" Text='<%# Bind("ProductId")%>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="tbInsertProdId" Width="50" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="lblProdId" runat="server" Text='<%# Bind("ProductId")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> 'shortened for brevity </Columns></asp:GridView>\[/code\]here is the code behind\[code\] Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) ' This just provides easier access to the Cells in the GridView Dim cells = GridView1.FooterRow.Cells Dim ctx As New teckEntities() Dim addInventory As New inventory() If e.CommandName = ("Insert") Then 'Create new Inventory object addInventory.ProductId = Convert.ToInt32(DirectCast(cells(3).FindControl("tbInsertProdId"), TextBox).Text) addInventory.Quantity = Convert.ToInt32(DirectCast(cells(4).FindControl("tbInsertQuantity"), TextBox).Text) addInventory.Location = Convert.ToString(DirectCast(cells(1).FindControl("tbInsertLocation"), TextBox).Text) 'attach the fields to the inventory context ' note: Id autoincrements and doesn't need to be set manually InsertInventoryItem(addInventory) 'need to call a gridview refresh here GridView1.DataBind() ElseIf e.CommandName = "Delete" Then 'Create new Inventory object **addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)** DeleteInventoryItem(Convert.ToInt32(addInventory.Id)) GridView1.DataBind() End IfEnd Sub\[/code\]I am getting the following error, why? The insert function works the same way and works properly.Object reference not set to an instance of an object.
 
Back
Top