473,385 Members | 2,029 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,385 software developers and data experts.

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.

Thanks,

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

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using ConnectionLibrary;

using System.Data.SqlClient;

namespace Affidavits

{

/// <summary>

/// Summary description for WebForm1.

/// </summary>

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.TextBox TextBox1;

protected System.Web.UI.WebControls.TextBox TextBox2;

protected System.Web.UI.WebControls.DataGrid DataGrid1;

protected System.Web.UI.HtmlControls.HtmlButton btnAddParcel;

public DataSet dsAssessorData;
private void Page_Load(object sender, System.EventArgs e)

{

// Put user code to initialize the page here

if(!IsPostBack)

{

// create a dataset

BuildDataSet();

// save as session variable

Session["dsAssr"] = dsAssessorData;

}

else

{

DataSet dsAssessorData = new DataSet("Parcels");

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

}

}

private void BuildDataSet()

{

DataTable dtParcels = new DataTable("tabParcels");

// add columns

DataColumn dcID = new DataColumn("ParcelID",typeof(string));

dcID.Unique = true;

dcID.AllowDBNull = false;

dtParcels.Columns.Add(dcID);
DataColumn dcAssessedValue = new DataColumn("Assessed", typeof(int));

dcAssessedValue.AllowDBNull = false;

dtParcels.Columns.Add(dcAssessedValue);

DataColumn dcAddress = new DataColumn("Address", typeof(string));

dcAddress.AllowDBNull = false;

dtParcels.Columns.Add(dcAddress);
DataSet dsAssessorData = new DataSet("Parcels");

dsAssessorData.Tables.Add(dtParcels);

// 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(rowNew);

DataGrid1.DataSource = dsAssessorData.Tables[0];

DataGrid1.DataBind();

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

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

//

InitializeComponent();

base.OnInit(e);

}
/// <summary>

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

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

/// </summary>

private void InitializeComponent()

{

this.btnAddParcel.ServerClick += new
System.EventHandler(this.btnAddParcel_ServerClick) ;

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

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

private void btnAddParcel_ServerClick(object sender, System.EventArgs e)

{

// connect to database and retrieve data

string strCn =
SCConnection.GetConnectionString(SCConnection.Asse ssor,false);

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

TextBox2.Text + "'";

SqlConnection cn = new SqlConnection(strCn);

cn.Open();

SqlCommand cmdGetAssrData = new SqlCommand(strSql,cn);

SqlDataReader drAssessor = cmdGetAssrData.ExecuteReader();

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

//DataSet dsAssessorData = new DataSet("Parcels");

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

while(drAssessor.Read())

{

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

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

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

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

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

", " + drAssessor.GetString(5);

dsAssessorData.Tables[0].Rows.Add(rowNew);

DataGrid1.DataSource = dsAssessorData.Tables[0];

DataGrid1.DataBind();

}

// update session variable

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

TextBox2.Text = "";

cn.Close();
}
}

}
Jul 19 '05 #1
2 6152
http://www.aspfaq.com/etiquette.asp?id=5002

Ray at work

"John Holmes" <jo****@co.skagit.wa.us> wrote in message
news:u3**************@tk2msftngp13.phx.gbl...

using System.Collections;

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.public.dotnet.framework.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

9
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
2
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...
14
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...
4
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...
3
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...
2
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...
4
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...
1
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...
2
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...
0
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
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...

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.