I have a DataTable from which I am trying to delete rows using the
DataGrid control. However, I'm having trouble maintaining the changes
to the DataTable between PostBacks. This is my first go 'round with
the DataGrid control, so pardon me if there are glaring mistakes...
public partial class control_panel_m ail_Default : System.Web.UI.P age
{
private DataTable dt;
protected void Page_Load(objec t sender, EventArgs e)
{
dt = new DataTable();
LoadContacts();
}
private void LoadContacts()
{
if (!IsPostBack)
{
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "blog_contact_f irstname";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_l astname";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_e mail";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
SqlConnection loConnection = new
SqlConnection(C onfigurationMan ager.AppSetting s["Data_Connectio n_String"].ToString());
SqlDataAdapter loAdapter = new SqlDataAdapter( "SELECT
blog_contact_fi rstname, blog_contact_la stname, blog_contact_em ail FROM
tbblog_contact WHERE blog_contact_de leted = 0 AND blog_contact_op tout =
0 ORDER BY blog_contact_fi rstname", loConnection);
loAdapter.Fill( dt);
gvRecipientList .DataSource = dt;
gvRecipientList .DataBind();
}
else
{
dt = ViewState["ContactLis t"] as DataTable;
gvRecipientList .DataSource = dt;
gvRecipientList .DataBind();
}
}
protected void gvRecipientList _RowDeleting(Ob ject sender,
GridViewDeleteE ventArgs e)
{
DataRow dr = dt.Rows[e.RowIndex];
dt.Rows.Remove( dr);
ViewState.Add(" ContactList", dt);
}
}
It fails on the statement:
DataRow dr = dt.Rows[e.RowIndex];
stating that the object reference not set to an instance of an
object... This happens even on the very first PostBack after the
initial load of the page.
Thanks in advance!
Jason
DataGrid control. However, I'm having trouble maintaining the changes
to the DataTable between PostBacks. This is my first go 'round with
the DataGrid control, so pardon me if there are glaring mistakes...
public partial class control_panel_m ail_Default : System.Web.UI.P age
{
private DataTable dt;
protected void Page_Load(objec t sender, EventArgs e)
{
dt = new DataTable();
LoadContacts();
}
private void LoadContacts()
{
if (!IsPostBack)
{
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "blog_contact_f irstname";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_l astname";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_e mail";
dc.DataType = System.Type.Get Type("System.St ring");
dt.Columns.Add( dc);
SqlConnection loConnection = new
SqlConnection(C onfigurationMan ager.AppSetting s["Data_Connectio n_String"].ToString());
SqlDataAdapter loAdapter = new SqlDataAdapter( "SELECT
blog_contact_fi rstname, blog_contact_la stname, blog_contact_em ail FROM
tbblog_contact WHERE blog_contact_de leted = 0 AND blog_contact_op tout =
0 ORDER BY blog_contact_fi rstname", loConnection);
loAdapter.Fill( dt);
gvRecipientList .DataSource = dt;
gvRecipientList .DataBind();
}
else
{
dt = ViewState["ContactLis t"] as DataTable;
gvRecipientList .DataSource = dt;
gvRecipientList .DataBind();
}
}
protected void gvRecipientList _RowDeleting(Ob ject sender,
GridViewDeleteE ventArgs e)
{
DataRow dr = dt.Rows[e.RowIndex];
dt.Rows.Remove( dr);
ViewState.Add(" ContactList", dt);
}
}
It fails on the statement:
DataRow dr = dt.Rows[e.RowIndex];
stating that the object reference not set to an instance of an
object... This happens even on the very first PostBack after the
initial load of the page.
Thanks in advance!
Jason
Comment