473,854 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_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

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

The first time when you do a postback, there is no ViewState["ContactLis t"]
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.IsPost Back) block.


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

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.goo glegroups.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_m ail_Default : System.Web.UI.P age
{
private DataTable dt;

protected void Page_Load(objec t sender, EventArgs e)
{
dt = new DataTable();
if (!IsPostBack)
{
LoadContacts();
}
else
{
dt = ViewState["ContactLis t"] as DataTable;
}
ViewState.Add(" ContactList", dt);
gvRecipientList .DataSource = dt;
gvRecipientList .DataBind();
}

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

protected void gvRecipientList _RowDeleting(Ob ject sender,
GridViewDeleteE ventArgs e)
{
dt = ViewState["ContactLis t"] 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
1810
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 code provided on the website. Once I have this example running correctly, I will need to use the concept in a more complex project. The code I am using involves an webform and an htc file. The code for ScrollPos.htc (which is located in a folder...
1
5438
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 information to bind mainly to a DataGrid, but I would also like it to persist for the duration that I have a particular page open. I seem to be losing this data either thru postbacks or by the way I have coded it. Does anyone have any suggestions on how...
4
3359
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 the user selects Rooms 1,5 and 7, and enters Bijoy for chairperson and clicks Add, a datagrid on the page should display the following:
1
2113
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
1182
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. Ideally, I'd like to be able to work with the list within a single page and not save the list until pressing an OK button. Should I keep a list of record IDs in a Session variable? Or postback the list of IDs in the Request? Is there a way to...
2
2631
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 defining a "sidebar" area that contains links to pages, and the content for those pages being displayed in a single content area. The sidebar is divided up into sections, that are show/hidden without postbacks using javascript. Here's an example:
2
3464
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 RowValue; RowValue = dt.Rows.ToString(); Response.Write(RowValue); Now when I run that code from within the same function that the datatable was populated from, it works.
5
3755
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 great, but if the user selects another item from the dropdown field and the sub runs again, the original data is overwritten. How do I store the original data and add the new datarow without overwriting the original? Here's my code: Protected...
5
5900
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 store a DataTable in any other way outside of our servers. In doing so, we leave ourselves open to large memory requirements. Furthermore, most web pages do not really support multiple changes per transaction. In other words, when the user submits...
0
10672
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9510
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7077
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.