Hi,
I have the error:
System.NullReferenceException: Object reference not set to an instance of an object
when i try to insert a new row in a grid view.
Below is the GridView:
<asp:GridView ID="gvTaskDetails" runat="server" AutoGenerateColumns="false" BackColor="LightGoldenrodYellow"
BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None" OnRowCommand="gvTaskDetails_rowCommand"
OnRowDataBound="gvTaskDetails_RowDataBound" style="width: 970px">
<Columns>
<asp:TemplateField HeaderText="Name" ItemStyle-CssClass="Item_txtGrid" HeaderStyle-CssClass="Item_txtGrid">
<ItemTemplate>
<asp:TextBox ID="txt_GridName" runat="server" Width="200px" Text='<%# Eval("NAME") %>' Visible="false" CssClass="textfield"></asp:TextBox>
<asp:Label ID="lblGrdName" Width="200px" runat="server" CssClass="Item_txtGrid" Text='<%# Eval("NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" ItemStyle-CssClass="Item_txtGrid" HeaderStyle-CssClass="Item_txtGrid">
<ItemTemplate>
<asp:TextBox ID="txt_GridDesc" runat="server" Width="200px" Text='<%# Eval("DESCRIPTION") %>' Visible="false" CssClass="textfield"></asp:TextBox>
<asp:Label ID="lblGrdDesc" Width="170px" runat="server" CssClass="Item_txtGrid" Text='<%# Eval("DESCRIPTION") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AssignedTo" ItemStyle-CssClass="Item_txtGrid" HeaderStyle-CssClass="Item_txtGrid">
<ItemTemplate>
<asp:TextBox ID="txt_GridAssigned" runat="server" Width="200px" Text='<%# Eval("ASSIGNEDTO") %>' Visible="false" CssClass="textfield"></asp:TextBox>
<asp:Label ID="lblGrdAssigned" Width="170px" runat="server" CssClass="Item_txtGrid" Text='<%# Eval("ASSIGNEDTO") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="StartDate" ItemStyle-CssClass="Item_txtGrid" HeaderStyle-CssClass="Item_txtGrid">
<ItemTemplate>
<asp:TextBox ID="txt_GridStartDate" runat="server" Width="200px" Text='<%# Eval("STARTDATE") %>' Visible="false" CssClass="textfield"></asp:TextBox>
<asp:Label ID="lblGrdStartDate" Width="170px" runat="server" CssClass="Item_txtGrid" Text='<%# Eval("STARTDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EndDate" ItemStyle-CssClass="Item_txtGrid" HeaderStyle-CssClass="Item_txtGrid">
<ItemTemplate>
<asp:TextBox ID="txt_GridEndDate" runat="server" Width="200px" Text='<%# Eval("ENDDATE") %>' Visible="false" CssClass="textfield"></asp:TextBox>
<asp:Label ID="lblGrdEndDate" Width="170px" runat="server" CssClass="Item_txtGrid" Text='<%# Eval("ENDDATE") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="lnkEdit" runat="server" AlternateText="Edit" CausesValidation="False" CommandName="EditClick" ImageUrl="~/Images/Proj_edit.gif" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<asp:ImageButton ID="lnkUpdate" runat="server" AlternateText="Update" CausesValidation="False" CommandName="UpdateClick" ImageUrl="~/Images/Proj_update.gif" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
<asp:ImageButton ID="lnkDelete" runat="server" AlternateText="Delete" CausesValidation="False" CommandName="DeleteClick" ImageUrl="~/Images/Proj_delete.gif" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="PaleGoldenrod" />
<FooterStyle BackColor="Tan" />
<HeaderStyle BackColor="Tan" Font-Bold="True" HorizontalAlign="Center" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
</asp:GridView>
Below is the code of AddToGrid function called in the event btnAddRowToGrid_Click:
private void AddToGrid()
{
try
{
DataTable dtable = new DataTable("ROW");
if (Session["ShareHolderDetails"] != null)
{
dtable = (DataTable)Session["ShareHolderDetails"];
}
else
{
dtable.Columns.Add(new DataColumn("NAME"));
dtable.Columns.Add(new DataColumn("DESCRIPTION"));
dtable.Columns.Add(new DataColumn("ASSIGNEDTO"));
dtable.Columns.Add(new DataColumn("STARTDATE"));
dtable.Columns.Add(new DataColumn("ENDDATE"));
}
DataRow drow = dtable.NewRow();
drow["NAME"] = string.Empty;
drow["DESCRIPTION"] = string.Empty;
drow["ASSIGNEDTO"] = string.Empty;
drow["STARTDATE"] = string.Empty;
drow["ENDDATE"] = string.Empty;
dtable.Rows.Add(drow);
gvTaskDetails.DataSource = dtable;
gvTaskDetails.DataBind();
foreach (GridViewRow gRow in gvTaskDetails.Rows)
{
Label lblGrdName = (Label)gRow.FindControl("lblGrdName");
Label lblGrdDescription = (Label)gRow.FindControl("lblGrdDescription");
Label lblGrdAssignedTo = (Label)gRow.FindControl("lblGrdAssignedTo");
Label lblGrdStartDate = (Label)gRow.FindControl("lblGrdStartDate");
Label lblGrdEndDate = (Label)gRow.FindControl("lblGrdEndDate");
TextBox txtGrdName = (TextBox)gRow.FindControl("txtGrdName");
TextBox txtGrdDescription = (TextBox)gRow.FindControl("txtGrdDescription");
TextBox txtGrdAssignedTo = (TextBox)gRow.FindControl("txtGrdAssignedTo");
TextBox txtGrdStartDate = (TextBox)gRow.FindControl("txtGrdStartDate");
TextBox txtGrdEndDate = (TextBox)gRow.FindControl("txtGrdEndDate");
ImageButton lnkEdit = (ImageButton)gRow.FindControl("lnkEdit");
ImageButton lnkUpdate = (ImageButton)gRow.FindControl("lnkUpdate");
ImageButton lnkDelete = (ImageButton)gRow.FindControl("lnkDelete");
if (lblGrdName.Text.ToString() == string.Empty)
{
txtGrdName.Visible = true;
txtGrdDescription.Visible = true;
txtGrdAssignedTo.Visible = true;
txtGrdStartDate.Visible = true;
txtGrdEndDate.Visible = true;
lblGrdName.Visible = false;
lblGrdDescription.Visible = false;
lblGrdAssignedTo.Visible = false;
lblGrdStartDate.Visible = false;
lblGrdEndDate.Visible = false;
lnkUpdate.Visible = true;
lnkEdit.Visible = false;
lnkDelete.Visible = true;
}
}
Session["ShareHolderDetails"] = dtable;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
Below is the code of showing the grid view in the load page:
public void ShowGrid()
{
DataTable dtable = new DataTable("ROW");
if (Session["ShareHolderDetails"] != null)
{
dtable = (DataTable)Session["ShareHolderDetails"];
}
else
{
dtable.Columns.Add(new DataColumn("NAME"));
dtable.Columns.Add(new DataColumn("DESCRIPTION"));
dtable.Columns.Add(new DataColumn("ASSIGNEDTO"));
dtable.Columns.Add(new DataColumn("STARTDATE"));
dtable.Columns.Add(new DataColumn("ENDDATE"));
}
gvTaskDetails.DataSource = dtable;
gvTaskDetails.DataBind();
}
Below is the code of the gridview RowDataBound:
protected void gvTaskDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton lnkDelete = (ImageButton)e.Row.FindControl("lnkDelete");
lnkDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure you want to delete the selected Task Details?');");
TextBox txtGrdName = (TextBox)e.Row.FindControl("txtGrdName");
TextBox txtGrdDescription = (TextBox)e.Row.FindControl("txtGrdDescription");
TextBox txtStartDate = (TextBox)e.Row.FindControl("txtStartDate");
TextBox txtEndDate = (TextBox)e.Row.FindControl("txtEndDate");
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
Below is the code of the gridview rowCommand:
protected void gvTaskDetails_rowCommand(object sender, GridViewCommandEventArgs e)
{
String ConnectionString = @"Data Source=SAFA-PC\SQLEXPRESS;Initial Catalog=ProjectMgnmt;Integrated Security=True";
SqlConnection SqlConn = new SqlConnection(ConnectionString);
try
{
SqlConn.Open();
int Index = Convert.ToInt32(e.CommandArgument);
GridViewRow gRow;
gRow = gvTaskDetails.Rows[Index];
Label lblGrdName = (Label)gRow.FindControl("lblGrdName");
Label lblGrdDescription = (Label)gRow.FindControl("lblGrdDescription");
Label lblGrdAssignedTo = (Label)gRow.FindControl("lblGrdAssignedTo");
Label lblGrdStartDate = (Label)gRow.FindControl("lblGrdStartDate");
Label lblGrdEndDate = (Label)gRow.FindControl("lblGrdEndDate");
TextBox txtGrdName = (TextBox)gRow.FindControl("txtGrdName");
TextBox txtGrdDescription = (TextBox)gRow.FindControl("txtGrdDescription");
TextBox txtGrdAssignedTo = (TextBox)gRow.FindControl("txtGrdAssignedTo");
TextBox txtGrdStartDate = (TextBox)gRow.FindControl("txtGrdStartDate");
TextBox txtGrdEndDate = (TextBox)gRow.FindControl("txtGrdEndDate");
ImageButton lnkEdit = (ImageButton)gRow.FindControl("lnkEdit");
ImageButton lnkUpdate = (ImageButton)gRow.FindControl("lnkUpdate");
ImageButton lnkDelete = (ImageButton)gRow.FindControl("lnkDelete");
if (e.CommandName == "EditClick")
{
txtGrdName.Visible = true;
txtGrdDescription.Visible = true;
txtGrdAssignedTo.Visible = true;
txtGrdStartDate.Visible = true;
txtGrdEndDate.Visible = true;
lblGrdName.Visible = false;
lblGrdDescription.Visible = false;
lblGrdAssignedTo.Visible = false;
lblGrdStartDate.Visible = false;
lblGrdEndDate.Visible = false;
lnkUpdate.Visible = true;
lnkEdit.Visible = false;
lnkDelete.Visible = false;
}
if (e.CommandName == "DeleteClick")
{
DataTable dTable1 = (DataTable)Session["ShareHolderDetails"];
dTable1.Rows.RemoveAt(Index);
gvTaskDetails.DataSource = dTable1;
gvTaskDetails.DataBind();
Session["ShareHolderDetails"] = dTable1;
}
if (e.CommandName == "UpdateClick")
{
txtGrdName.Visible = false;
txtGrdDescription.Visible = false;
txtGrdAssignedTo.Visible = false;
txtGrdStartDate.Visible = false;
txtGrdEndDate.Visible = false;
lblGrdName.Visible = true;
lblGrdDescription.Visible = true;
lblGrdAssignedTo.Visible = true;
lblGrdStartDate.Visible = true;
lblGrdEndDate.Visible = true;
lnkUpdate.Visible = false;
lnkEdit.Visible = true;
lnkDelete.Visible = true;
DataTable dTable1 = (DataTable)Session["ShareHolderDetails"];
dTable1.Rows[Index]["DESCRIPTION"] = txtGrdDescription.Text;
dTable1.Rows[Index]["ASSIGNEDTO"] = txtGrdAssignedTo.Text;
dTable1.Rows[Index]["STARTDATE"] = txtGrdStartDate.Text;
dTable1.Rows[Index]["ENDDATE"] = txtGrdEndDate.Text;
gvTaskDetails.DataSource = dTable1;
gvTaskDetails.DataBind();
Session["ShareHolderDetails"] = dTable1;
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
SqlConn.Close();
}
}
Please Help..!!!
Thanks in advance