473,549 Members | 2,825 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem maintaining dataset in session variable

I have a web interface where the user types in ID's one at a time. After an
ID is typed in, a button is clicked and the button click event has code that
does a query and returns a data reader and then appends the data to a
dataset that is built in the Page_Load code in the if(!isPostBack) block.
When I try to add a row in the button click event code I get an error saying
that "Object reference not set to an instance of an object". I'm saving the
dataset in a Session object and then restoring it in the else clause of the
if(!isPostBack) block. I've pasted code below, please help. I'm relatively
new at asp.net development but have lots of asp and ado experience.


John Holmes
--------------------------------- code ----------------------------------
using System;

using System.Collecti ons;

using System.Componen tModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.Sess ionState;

using System.Web.UI;

using System.Web.UI.W ebControls;

using System.Web.UI.H tmlControls;

using ConnectionLibra ry;

using System.Data.Sql Client;

namespace Affidavits


/// <summary>

/// Summary description for WebForm1.

/// </summary>

public class WebForm1 : System.Web.UI.P age


protected System.Web.UI.W ebControls.Text Box TextBox1;

protected System.Web.UI.W ebControls.Text Box TextBox2;

protected System.Web.UI.W ebControls.Data Grid DataGrid1;

protected System.Web.UI.H tmlControls.Htm lButton btnAddParcel;

public DataSet dsAssessorData;
private void Page_Load(objec t sender, System.EventArg s e)


// Put user code to initialize the page here



// create a dataset


// save as session variable

Session["dsAssr"] = dsAssessorData;




DataSet dsAssessorData = new DataSet("Parcel s");

dsAssessorData = (DataSet) Session["dsAssr"];



private void BuildDataSet()


DataTable dtParcels = new DataTable("tabP arcels");

// add columns

DataColumn dcID = new DataColumn("Par celID",typeof(s tring));

dcID.Unique = true;

dcID.AllowDBNul l = false;

dtParcels.Colum ns.Add(dcID);
DataColumn dcAssessedValue = new DataColumn("Ass essed", typeof(int));

dcAssessedValue .AllowDBNull = false;

dtParcels.Colum ns.Add(dcAssess edValue);

DataColumn dcAddress = new DataColumn("Add ress", typeof(string)) ;

dcAddress.Allow DBNull = false;

dtParcels.Colum ns.Add(dcAddres s);
DataSet dsAssessorData = new DataSet("Parcel s");

dsAssessorData. Tables.Add(dtPa rcels);

// add a test row

DataRow rowNew = dsAssessorData. Tables[0].NewRow();

rowNew["ParcelID"] = "P34031";

rowNew["Assessed"] = "255000";

rowNew["Address"] = "88493" + " " + "Nowhere Road" +

", " + "Everywhere , WA 98834";

dsAssessorData. Tables[0].Rows.Add(rowNe w);

DataGrid1.DataS ource = dsAssessorData. Tables[0];

DataGrid1.DataB ind();


#region Web Form Designer generated code

override protected void OnInit(EventArg s e)



// CODEGEN: This call is required by the ASP.NET Web Form Designer.


InitializeCompo nent();


/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeCompo nent()


this.btnAddParc el.ServerClick += new
System.EventHan dler(this.btnAd dParcel_ServerC lick);

this.Load += new System.EventHan dler(this.Page_ Load);



//************* error occurs in this click event

private void btnAddParcel_Se rverClick(objec t sender, System.EventArg s e)


// connect to database and retrieve data

string strCn =
SCConnection.Ge tConnectionStri ng(SCConnection .Assessor,false );

string strSql = "select * from ExciseAffidavit View where [parcel number] =
'" +

TextBox2.Text + "'";

SqlConnection cn = new SqlConnection(s trCn);


SqlCommand cmdGetAssrData = new SqlCommand(strS ql,cn);

SqlDataReader drAssessor = cmdGetAssrData. ExecuteReader() ;

// I tried adding this code, but it didn't help

//DataSet dsAssessorData = new DataSet("Parcel s");

//dsAssessorData = (DataSet) Session["dsAssr"];

while(drAssesso r.Read())


//*************** error occurs on the following line

DataRow rowNew = dsAssessorData. Tables[0].NewRow();

rowNew["ParcelID"] = drAssessor.GetS tring(0);

rowNew["Assessed"] = drAssessor.GetS tring(15);

rowNew["Address"] = drAssessor.GetS tring(3) + " " + drAssessor.GetS tring(4)

", " + drAssessor.GetS tring(5);

dsAssessorData. Tables[0].Rows.Add(rowNe w);

DataGrid1.DataS ource = dsAssessorData. Tables[0];

DataGrid1.DataB ind();


// update session variable

Session["dsAssr"] = dsAssessorData;
TextBox1.Text += TextBox2.Text;

TextBox2.Text = "";


Jul 19 '05 #1
2 6160

Ray at work

"John Holmes" <jo****@co.skag it.wa.us> wrote in message
news:u3******** ******@tk2msftn gp13.phx.gbl...

using System.Collecti ons;

Jul 19 '05 #2
Hello John,

Thanks for posting in the group.

Based on my understanding, now the question is: You stored a dataset in a
session in asp.net application. However, you got "Object reference not set
to an instance of an object" when the page is executing.

After reviewing your code, I found that in the else block, dsAssessorData
is defined as a local variable. So it will not be valid beyond the else
block. That could cause some potenial erros if we still want to use this
variable later.

BTW, in the future, it would be best to post these questions in the
following newsgroup.

Microsoft.publi c.dotnet.framew ork.aspnet

All .NET ASP.NET issues, configuration and other questions are posted in
the newsgroup above.

The reason why we recommend posting appropriately is you will get the most
qualified pool of respondents, and other partners who the newsgroups
regularly can either share their knowledge or learn from your interaction
with us. Also, this is to make sure that the responders can better track
the problem. Thank you for your understanding.

Thanks again for using Microsoft MSDN Newsgroups.

Have a nice day!

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Jul 19 '05 #3

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

Similar topics

by: Steven Blair | last post by:
Hi, Here is my problem: This is using Visual Studio 2005 I have a DataSet declared as a public member: public DataSet tmp
by: John Holmes | last post by:
I have a web interface where the user types in ID's one at a time. After an ID is typed in, a button is clicked and the button click event has code that does a query and returns a data reader and then appends the data to a dataset that is built in the Page_Load code in the if(!isPostBack) block. When I try to add a row in the button click...
by: Venkat Chellam | last post by:
I have a peculiar problem. I have a simple web application which loads some data from the oracle table and display in the datagrid in the webpage and datagrid has page enabled which shows 10 rows at a page.I have a search criteria to search the records based on the data range i give This is what i have done, in the !IsPostBack section. I...
by: Huw Lloyd | last post by:
I have developed a windows application in VB.NET and now want to take parts of it and put it on the web, so although I understand all about datasets etc, I know little about web development My issue is that I have a grid bound to a dataset, and I have a text box under the grid where users can enter a new value. They then click the ADD button...
by: Aaron | last post by:
Why do my session values return to nothing on post back? I want to click a button and have the row, as in: dataset.table(0).rows(THIS ROW NUMBER IS WHAT I AM TALKING ABOUT), either increment or decrement to facilitate a movenext and movelast. I have been pulling my hair out and am at the end of my rope. PLEASE, how should I implement...
by: asadikhan | last post by:
Hi, I have a table in my database called users which contains user information like their login id, password, first name, last name, phone, etc. Once I retreive this information, I'd like to hold it in memory. One obvious way of doing this is session variables. However, I am thinking whether usign a Dataset would be more suitable in this...
by: Sam | last post by:
I have an asp.net 2.0 app that uses a sitemap, Master Page, and has several content pages. While this feature has simplified the process of creating a data-driven site menu, it does seem to have some big restrictions. Namely, maintaining state on the Master Page across all content pages. I simply want to keep track of a key value across all...
by: whidbey | last post by:
Hello friends, I am whidbey, new to thescripts and dot net as well.I am working over Online Shopping Cart,web application.I design a page (webform5.aspx) where user search books then select the books to buy from the datagrid. User can select books using checkboxes in the datagrid. The selected books are stored in a session variable name...
by: RoFaR | last post by:
I have a dataset, dinamically I create a table and fill them with data, then I add the table to the dataset, The dataset is stored in a session variable. Later When I access the dataset from the session variable I lost all the changes I did... Im using microsoft ajax, thanks for the help.
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb"...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
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...
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...
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...
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...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.