Thanks, this helped ... john
"Steven Cheng[MSFT]" <v-schang@online.microsoft.com> wrote in message
news:LhvV0YozDHA.3532@cpmsftngxa07.phx.gbl...[color=blue]
> Hi John Holmes,
>
>
> Thank you for using Microsoft Newsgroup Services. Based on your[/color]
desciption,[color=blue]
> you are dealing a ASP.NET web page ,the page loaded a DataSet on first
> request and stored it into Session, then in the later request(such as
> button click post back.), retrieve the dataset from the Session and do[/color]
some[color=blue]
> operation(such as add a row). However, you encountered some errors when[/color]
run[color=blue]
> the page, seems the dataset isn't set to the Session. Please correct me if
> my understanding it not quite exact.
>
> After reading the page's code you provided, I found that there is[/color]
something[color=blue]
> incorrect in it:
> you define a DataSet in the page class as a public member as below:
> public DataSet dsAssessorData;
>
> however, in the BuildDataSet function, you defined another function scope
> local DataSet vairable:
> private void BuildDataSet()
>
> {
>
> ........................
>
>
> DataSet dsAssessorData = new DataSet("Parcels");
>
> .........................
>
> }
>
> Also, in the Page_Load , you code like this:
> .................
> if(!IsPostBack)
>
> {
> // create a dataset
> BuildDataSet();
>
> // save as session variable
> Session["dsAssr"] = dsAssessorData;
> }
> ......................
>
> the "dsAssessorData" you set to the Session["dsAssr"] is the public class
> member DataSet, not the local DataSet variable,
> So it is set to "null" which caused the "Null reference" error. You may
> set the new DataSet to the public class membe DataSet in BuildDataSet()
> function.
>
> In addtion, to make it clear, I've written a simple page showing how to
> create a DataSet at first request and then store in Session for later
> retrieving, here is the source:
>
> ---------------ASPX PAGE ---------------
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
> <HTML>
> <HEAD>
> <title>SessionDataSet</title>
> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
> <meta name="CODE_LANGUAGE" Content="C#">
> <meta name="vs_defaultClientScript" content="JavaScript">
> <meta name="vs_targetSchema"
> content="http://schemas.microsoft.com/intellisense/ie5">
> </HEAD>
> <body>
> <form id="Form1" method="post" runat="server">
> <table width="500" align="center">
> <tr>
> <td>
> <asp:DataGrid id="dgSession" runat="server"></asp:DataGrid>
> </td>
> <td>
> </td>
> </tr>
> <tr>
> <td>
> <asp:Button id="btnAdd" runat="server" Text="AddRow"></asp:Button>
> </td>
> <td>
> </td>
> </tr>
> </table>
> </form>
> </body>
> </HTML>
>
> --------------------------CODE BIND
> ASPX.CS-------------------------------------
> 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;
>
> namespace NewWebApp
> {
> /// <summary>
> /// Summary description for SessionDataSet.
> /// </summary>
> public class SessionDataSet : System.Web.UI.Page
> {
> protected System.Web.UI.WebControls.DataGrid dgSession;
> protected System.Web.UI.WebControls.Button btnAdd;
> protected DataSet dsTemp = null;
>
> private void Page_Load(object sender, System.EventArgs e)
> {
> // Put user code to initialize the page here
> if(!IsPostBack)
> {
> dsTemp = GetDataSet();
> dgSession.DataSource = dsTemp;
> dgSession.DataBind();
> Session["DATASET"] = dsTemp;
> }
> else
> {
> dsTemp = (DataSet)Session["DATASET"];
> }
>
> }
>
> protected DataSet GetDataSet()
> {
> DataSet ds = new DataSet("dsSession");
> DataTable tb = new DataTable("tbSession");
>
> tb.Columns.Add("id");
> tb.Columns.Add("name");
> tb.Columns.Add("email");
>
> for(int i=0;i<20;i++)
> {
> DataRow newrow = tb.NewRow();
> newrow["id"] = i.ToString();
> newrow["name"] = "name" + i.ToString();
> newrow["email"] = "email" + i.ToString();
>
> tb.Rows.Add(newrow);
> }
>
> ds.Tables.Add(tb);
>
> return ds;
> }
>
> #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.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
> this.Load += new System.EventHandler(this.Page_Load);
>
> }
> #endregion
>
> private void btnAdd_Click(object sender, System.EventArgs e)
> {
> DataTable tb = dsTemp.Tables["tbSession"];
> DataRow newrow = tb.NewRow();
>
> Random rnd = new Random();
> int rval = rnd.Next();
>
> newrow["id"] = rval.ToString();
> newrow["name"] = "name" + rval.ToString();
> newrow["email"] = "email" + rval.ToString();
>
> tb.Rows.Add(newrow);
>
> dgSession.DataSource = dsTemp;
> dgSession.DataBind();
>
> }
> }
> }
>
>
> Please try the preceding suggestion and let me know if you have any
> question on it.
>
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure!
www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>[/color]