473,320 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Maintain a DataTable between postbacks...

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

May 10 '06 #1
4 8500
I might be wrong...

The first time when you do a postback, there is no ViewState["ContactList"]
yet it seems. when it gets to the deleting part, i imagine there is nothing
for it to delete.

You should add ViewState.Add("ContactList", dt) into your
if(!Page.IsPostBack) block.


"Jason" <ja*************@hotmail.com> wrote in message
news:11**********************@q12g2000cwa.googlegr oups.com...
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

May 10 '06 #2
You are correct. When the page is initially loaded, (!IsPostBack) is a
true condition, so, it grabs the information from the database. That
is obvious because the GridView is populated with the correct data upon
initial load.

But, it is when you enter gvRecipientList_RowDeleting for the first
time, it's saying that "dt" is not set to an instance of an object.
That's what I'm not understanding at the moment...

May 10 '06 #3
So did it work for you or ..?

1. First time enter in the page, gridview binded directly to the data from
db.
2. PostBack, Gridview binded to a null ViewState object, so did "dt".
3. Enter gvRecipientList_RowDeleting, you try to retrieve "dt".Rows... but
"dt" is null at this time.
"Jason" <ja*************@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
You are correct. When the page is initially loaded, (!IsPostBack) is a
true condition, so, it grabs the information from the database. That
is obvious because the GridView is populated with the correct data upon
initial load.

But, it is when you enter gvRecipientList_RowDeleting for the first
time, it's saying that "dt" is not set to an instance of an object.
That's what I'm not understanding at the moment...

May 10 '06 #4
I don't know if this is a proper solution, but I got this to work:

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();
if (!IsPostBack)
{
LoadContacts();
}
else
{
dt = ViewState["ContactList"] as DataTable;
}
ViewState.Add("ContactList", dt);
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}

private void LoadContacts()
{
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();
}

protected void gvRecipientList_RowDeleting(Object sender,
GridViewDeleteEventArgs e)
{
dt = ViewState["ContactList"] as DataTable;
dt.Rows.Remove(dt.Rows[e.RowIndex]);
ViewState.Add("ContactList", dt);
gvRecipientList.DataSource = dt;
gvRecipientList.DataBind();
}
}

May 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: GrantS | last post by:
I am trying to convert the VB.Net code example povided by http://authors.aspalliance.com/JimRoss/Articles/MaintainScrollPos.aspx into C# (ASP.Net)without success. No errors are thrown in the VB...
1
by: Paul Perot | last post by:
Hi All: I have a DataTable that I have defined Globally. I populate this datatable dynamically with file/folder information that I read directly from the server. I use this datatable...
4
by: bijoy | last post by:
My page has a Listbox called Rooms, a text field called chairPerson and a button called Add. When Add is clicked, the script needs to display the selected info in a datagrid. For example, if...
1
by: keithb | last post by:
Can someone provide general information on how to maintain state for a treeview control. Specifically, I need the application to maintain node expansion across postbacks. Thanks, Keith
2
by: Todd | last post by:
On an Asp.Net webpage, what's the best way to go about managing a list of items? When editing a library title I display a DataGrid of authors to which I'd like to be able to add and remove. ...
2
by: Evan M. | last post by:
Hello there, I have an interesting problem to takle. I'm creating a website that's going to be run in a local Intranet. The site uses a MasterPage / content page scheme, with the Master page...
2
by: DubSport | last post by:
I have created a datatable in a function, and it is populated with data. I want to call a new function from an ASP button, and write out some of the values in the datatable, using: string...
5
by: =?Utf-8?B?SnVsaWEgQg==?= | last post by:
Hi, I wonder if anyone can help? I've got a web form (intranet), .net version 1.1. I've got a sub that populates a datatable in a dataset, dependent on a dropdown field selection. This works...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.