hii alll,
i am working on very simple gridview control.i m able to edit update n cancel bue AS SOON AS I CLICK ON DELETE :I GOT EXCEPTION AS :

Object reference not set to an instance of an object.

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
  TextBox empno = (TextBox)row.FindControl("txtEmployeeNo");
        

        String ConnectionString = "server=XYZ;database=db_test;user id=sa;password=ABC;";
        SqlConnection Conn = new SqlConnection(ConnectionString);
        Conn.Open();
        SqlCommand cmd = new SqlCommand("delete from  tab1 where EmployeeNo='" + empno.Text + "'", Conn);//error is here
        cmd.ExecuteNonQuery();
        Conn.Close();
        bind();

    }
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            AutoGenerateDeleteButton="True" DataKeyNames="EmployeeNo" AutoGenerateEditButton="True"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
            OnRowCancelingEdit="GridView1_RowCancelingEdit" PageSize="3" OnPageIndexChanging="GridView1_PageIndexChanging"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:TemplateField HeaderText="EmployeeNo">
                    <ItemTemplate>
                        <%#Eval("EmployeeNo")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmployeeNo" runat="server" Text=' <%#Eval("EmployeeNo")%> '> </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <%#Eval("Name")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtName" runat="server" Text=' <%#Eval("Name")%> '> </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Nick">
                    <ItemTemplate>
                        <%#Eval("Nick")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtNick" runat="server" Text=' <%#Eval("Nick")%> '> </asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

I HAVE TAKEN EmployeeNo as primary key.

also tell in which scenario we use column index value........
means can i write :

GridViewRow row = GridView.Rows[e.RowIndex];
TextBox empno = (TextBox)row.Cells[0].Controls[0];

thanks in advance

Recommended Answers

All 5 Replies

GridViewRow row = GridView.Rows[e.RowIndex];
TextBox empno = (TextBox)row.Cells[0].FindControl("txtEmployeeNo");

if(empno==null)
  return;
....

First off - Don't EVER do this EmployeeNo='" + empno.Text + "'" look up parameterized queries and stored procedures.

In your code behind you are calling bind() after the connection has been closed. I believe that is your error.

In this case you can do what you want to do quick and easy with just a SqlDataSource and a Gridview without the code behind.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConflictDetection="CompareAllValues" 
        ConnectionString="<%$ ConnectionStrings:DaniWebConnectionString %>" 
        DeleteCommand="DELETE FROM [tab1] WHERE [EmployeeNo] = @original_EmployeeNo AND [Name] = @original_Name AND [Nick] = @original_Nick" 
        InsertCommand="INSERT INTO [tab1] ([EmployeeNo], [Name], [Nick]) VALUES (@EmployeeNo, @Name, @Nick)" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCommand="SELECT [EmployeeNo], [Name], [Nick] FROM [tab1]" 
        
        UpdateCommand="UPDATE [tab1] SET [Name] = @Name, [Nick] = @Nick WHERE [EmployeeNo] = @original_EmployeeNo AND [Name] = @original_Name AND [Nick] = @original_Nick">
        <DeleteParameters>
            <asp:Parameter Name="original_EmployeeNo" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_Nick" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Nick" Type="String" />
            <asp:Parameter Name="original_EmployeeNo" Type="Int32" />
            <asp:Parameter Name="original_Name" Type="String" />
            <asp:Parameter Name="original_Nick" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="EmployeeNo" Type="Int32" />
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Nick" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="EmployeeNo" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo" ReadOnly="True" 
                    SortExpression="EmployeeNo" />
                <asp:BoundField DataField="Name" HeaderText="Name" 
                    SortExpression="Name" />
                <asp:BoundField DataField="Nick" HeaderText="Nick" 
                    SortExpression="Nick" />
            </Columns>
        </asp:GridView>

thanks for quick response.....i am new just in asp.net .i have taken empno as primarykey(in ma database) how can it be null...........plzz make me right if i am wrong /.........

>i am new just in asp.net .i have taken empno as primarykey how can it be null...........plzz make me right if i am wrong

Statement in post #2 check the existence of txtEmployeeNo control in 1st cell.

TextBox empno = (TextBox)row.Cells[0].FindControl("txtEmployeeNo");

if(empno==null)
  return;

Hi,,

I think your problem is this line, you are looking for a employeeno text box , instead you should look for label control which displays employee no, coz when you r not in itemtemplate edit mode the values will be displayed by label controls in normal cases

TextBox empno = (TextBox)row.Cells[0].FindControl("txtEmployeeNo");

so try

label empno = (TextBox)row.Cells[0].FindControl("you label id");

Mark as solved if it helps you!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.