Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8615041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:16:26+00:00 2026-06-12T05:16:26+00:00

I have the following aspx code; <asp:GridView ID=GridView1 runat=server AllowPaging=True AllowSorting=True AutoGenerateColumns=False DataKeyNames=ProductId DataSourceID=edsinventory

  • 0

I have the following aspx 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>

here is the code behind

 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

        **addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)**

        DeleteInventoryItem(Convert.ToInt32(addInventory.Id))
        GridView1.DataBind()
    End If

End Sub

I am getting the following error on the deleting routine where the **, why? The insert function works the same way and works properly.
Object reference not set to an instance of an object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:16:27+00:00Added an answer on June 12, 2026 at 5:16 am

    I think the problem is you’re looking the Label lblEditId in Footer controls:

    Dim cells = GridView1.FooterRow.Cells
    ...
    addInventory.Id = Convert.ToInt32(DirectCast(cells(3).FindControl("lblEditId"), Label).Text)
    

    Try this instead:

    Update your delete linkbutton to (add the ID to the CommandArgument):

    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"  CommandArgument='<%#Eval("Id")%>' Text="Delete"></asp:LinkButton>
    

    Then, replace your ** code with this:
    Note: Based on the code you provided, it seem like the lblEditId is in column 2 hence the Cell(1) – zero based index, instead of Cell(3).

    addInventory.Id = Convert.ToInt32(DirectCast(GridView1.Rows(e.CommandArgument).Cells(1).FindControl("lblEditId"), Label).Text)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following aspx: <asp:GridView ID=GridView1 runat=server AutoGenerateColumns=False BorderStyle=None Font-Names=Verdana Font-Size=12px OnSelectedIndexChanged=GridView1_SelectedIndexChanged ShowHeader=False
The .aspx: <asp:GridView ID=gvFirst runat=server AutoGenerateColumns=false AllowPaging=true ondatabound=gvFirst_DataBound > <Columns> <asp:BoundField DataField=ID HeaderText=ProductID/> <asp:BoundField
I have the following ASP code: <asp:LinkButton ID=LinkButton1 runat=server Text=edit item onclick='AddItem.aspx?catid=<%# Eval(CollectionID)%>' />
I have the following structure in an aspx page: <asp:Panel ID=pnlCust runat=server> <asp:GridView ID=gvMaster
I have the following ASPX code: <asp:ScriptManager ID=ScriptManager1 runat=server /> <asp:UpdatePanel runat=server ID=UpdatePanel UpdateMode=Conditional>
I have following code: <asp:Button ID=InsertButton runat=server CausesValidation=True CommandName=Insert Text=Add Form PostBackUrl=~/APPLICATION/FormView.aspx?postquestion=/> I using
I have the following listbox on aspx page. <asp:ListBox runat=server ID=lbA Visible=true SelectionMode=Multiple DataTextField=A_FACTOR
i've the following code in aspx page <asp:Label ID=CittaLabel runat=server Text='<%# Eval(Citta) %>' Font-Size='<%#
I have the following button on a .aspx page: <asp:Button runat=server ID=bntLogin OnClick=bntLogin_Click Text=Login
I have a follow dropdown list in my aspx page: <asp:DropDownList ID=OrderPeriodStatus runat=server AutoPostBack=true

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.