J
Jason
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_mail_Default : System.Web.UI.Page
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
LoadContacts();
}
private void LoadContacts()
{
if (!IsPostBack)
{
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "blog_contact_firstname";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_lastname";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_email";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlDataAdapter loAdapter = new SqlDataAdapter("SELECT
blog_contact_firstname, blog_contact_lastname, blog_contact_email FROM
tbblog_contact WHERE blog_contact_deleted = 0 AND blog_contact_optout =
0 ORDER BY blog_contact_firstname", loConnection);
loAdapter.Fill(dt);
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}
else
{
dt = ViewState["ContactList"] as DataTable;
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}
}
protected void gvRecipientList_RowDeleting(Object sender,
GridViewDeleteEventArgs 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_mail_Default : System.Web.UI.Page
{
private DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
dt = new DataTable();
LoadContacts();
}
private void LoadContacts()
{
if (!IsPostBack)
{
DataColumn dc;
dc = new DataColumn();
dc.ColumnName = "blog_contact_firstname";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_lastname";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
dc = new DataColumn();
dc.ColumnName = "blog_contact_email";
dc.DataType = System.Type.GetType("System.String");
dt.Columns.Add(dc);
SqlConnection loConnection = new
SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
SqlDataAdapter loAdapter = new SqlDataAdapter("SELECT
blog_contact_firstname, blog_contact_lastname, blog_contact_email FROM
tbblog_contact WHERE blog_contact_deleted = 0 AND blog_contact_optout =
0 ORDER BY blog_contact_firstname", loConnection);
loAdapter.Fill(dt);
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}
else
{
dt = ViewState["ContactList"] as DataTable;
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}
}
protected void gvRecipientList_RowDeleting(Object sender,
GridViewDeleteEventArgs 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