When i updating dropdownlist in gridview, having this error.
Object reference not set to an instance of an object
string NamDept = (GridView.Rows[e.RowIndex].FindControl("ddIDDEPT") as DropDownList).SelectedItem.Value;
Here is complete coding
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AutoGenerateEditButton="True" OnRowDataBound="GridView1_RowDataBound"
OnRowEditing="GridView1_RowEditing" DataKeyNames="EmpID"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CssClass="gridv" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Department">
<EditItemTemplate>
<asp:DropDownList id="ddlIDDEPT" CssClass="dropdown" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ddlIDDEPT" runat="server" Text='<% #Eval("NamDept") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubDepartment">
<EditItemTemplate>
<asp:DropDownList id="ddlSubDepartment" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ddlSubDepartment" runat="server" Text='<% #Eval("NamSubDept") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Designation">
<EditItemTemplate>
<asp:DropDownList id="ddlDesignation" runat="server" Text='<% #Eval("NamDesig") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="ddlDesignation" runat="server" Text='<% #Eval("NamDesig") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string NamDept = (GridView1.Rows[e.RowIndex].FindControl("ddlDepartment") as DropDownList).SelectedItem.Value;
int IDdept = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
SqlConnection con = new SqlConnection("Data Source=IT;Integrated Security=SSPI;Initial Catalog=AttendanceManagement");
{
string query = "UPDATE Department SET NamDept = @NamDept WHERE IDdept = @IDdept";
SqlCommand cmd = new SqlCommand(query, con);
{
cmd.Parameters.AddWithValue("@NamDept", NamDept);
cmd.Parameters.AddWithValue("@IDdept", IDdept);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
pleas guide ...