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