473,385 Members | 1,343 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.

Passing dataset between ASP.NET Web Forms

I am going to past a dataset from First.aspx to Second.aspx.
A whole table will be displayed on First.aspx and partial columns
will be displayed on Second.aspx.
First.aspx view in browser works well if I exclude Second.aspx. But when Second.aspx is included in the project, an error happens.
I can't find what is wrong in my code.

Expand|Select|Wrap|Line Numbers
  1.  System.NullReferenceException was unhandled by user code
  2.   Message="Object reference not set to an instance of an object."
First.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. namespace FirstPage
  2. {
  3.     public partial class GetStrings : System.Web.UI.Page
  4.     {
  5.         getStrings g = new getStrings();
  6.         protected void Page_Load(object sender, EventArgs e)
  7.         {
  8.             string strConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
  9.             SqlConnection myConnection = new SqlConnection(strConnectionString);
  10.             DataSet myDataSet = new DataSet();
  11.             try
  12.             {
  13.                 myConnection.Open();
  14.                 // create the data
  15.                 GenerateDataSet(myDataSet, myConnection);
  16.             }
  17.             finally
  18.             {
  19.                 myConnection.Close();
  20.                 // bind each to table to a grid
  21.  
  22.             }
  23.             Session["myds"] = myDataSet;
  24.             // databind the page 
  25.             GridView1.DataSource = myDataSet.Tables["Weather"];
  26.             GridView1.DataBind();
  27.         }
and
Second.aspx.cs
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using FirstPage;
  12.  
  13. public partial class Second : System.Web.UI.Page
  14. {
  15.     protected void Page_Load(object sender, EventArgs e)
  16.     {
  17.         DataSet ds = (DataSet)Session["myds"];
  18.         FillTable(ds);
  19.         GridView1.DataSource = ds.Tables["Index"];
  20.         GridView1.DataBind();
  21.     }
  22.  
  23.     void FillTable(DataSet dset)
  24.     {
  25.         DataTable indexTable = dset.Tables.Add("Index");
  26.  
  27.         DataColumn indexID =
  28.             indexTable.Columns.Add("State", typeof(string));
  29.         indexTable.Columns.Add("Division", typeof(string));
  30.         indexTable.Columns.Add("MOIST_INDEX", typeof(string));
  31.         indexTable.Columns.Add("DROUGHT_INDEX", typeof(string));
  32.     }
  33. }
Second.aspx
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="Second" %>
  2. <%@ Import Namespace="System.Data" %>
  3. <%@ Import Namespace="System.Data.SqlClient" %>
  4. <%@ Reference Page="First.aspx" %>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6.  
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head id="Head1" runat="server">
  9.     <title>Untitled Page</title>
  10. </head>
  11. <body>
  12.     <form id="form1" runat="server">
  13.     <div>
  14.         <asp:GridView ID="GridView1" runat="server">
  15.         </asp:GridView>
  16.     </div>
  17.     </form>
  18. </body>
  19. </html>
Any help will be appreciated.
Feb 9 '08 #1
2 2989
nateraaaa
663 Expert 512MB
Second.aspx.cs

Code: ( text )
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FirstPage;

public partial class Second : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = (DataSet)Session["myds"];
FillTable(ds);
GridView1.DataSource = ds.Tables["Index"];
GridView1.DataBind();
}

void FillTable(DataSet dset)
{
DataTable indexTable = dset.Tables.Add("Index");

DataColumn indexID =
indexTable.Columns.Add("State", typeof(string));
indexTable.Columns.Add("Division", typeof(string));
indexTable.Columns.Add("MOIST_INDEX", typeof(string));
indexTable.Columns.Add("DROUGHT_INDEX", typeof(string));
}
}
I believe what is happening is table Index does not exist in your dataset so GridView1.DataSource = ds.Tables["Index"];
is creating an object reference not set to an instance of an object error. There are a few ways to change your code to eliminate this error. Here a 2 options.
1. Change your parameter to include the out keyword
Expand|Select|Wrap|Line Numbers
  1. public void FillTable(out DataSet dset)
  2.     {
  3.         DataTable indexTable = dset.Tables.Add("Index");
  4.  
  5.         DataColumn indexID =
  6.             indexTable.Columns.Add("State", typeof(string));
  7.         indexTable.Columns.Add("Division", typeof(string));
  8.         indexTable.Columns.Add("MOIST_INDEX", typeof(string));
  9.         indexTable.Columns.Add("DROUGHT_INDEX", typeof(string));
  10.     }
2. Change your method to return a DataSet and then add the DataTable to your DataSet.
Expand|Select|Wrap|Line Numbers
  1. public DataSet FillTable(DataSet dset)
  2.     {
  3.         DataTable indexTable = dset.Tables.Add("Index");
  4.  
  5.         DataColumn indexID =
  6.             indexTable.Columns.Add("State", typeof(string));
  7.         indexTable.Columns.Add("Division", typeof(string));
  8.         indexTable.Columns.Add("MOIST_INDEX", typeof(string));
  9.         indexTable.Columns.Add("DROUGHT_INDEX", typeof(string));
  10.  
  11.         return dset;
  12.     }
  13.  protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         DataSet ds = (DataSet)Session["myds"];
  16.         ds = FillTable(ds);
  17.         GridView1.DataSource = ds.Tables["Index"];
  18.         GridView1.DataBind();
  19.     }
  20.  
Nathan
Feb 11 '08 #2
Thanks!
I will try it.
Feb 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Cantekin Guneser | last post by:
i am new to windows aplication, i am planning to desing a program which has login form. i want to use in formation at second form which i used in login form. ex: i filled a dataset in login form...
3
by: JJ | last post by:
Hi, I need to pass a dataset to another win form along with a SqldataAdapter. I don't want to recreate the SqlDataAdapter again either. So to pass to another Win form in my windows form app, do...
19
by: Jaime Stuardo | last post by:
Hi all.. I have created a business logic component that is used from my ASP.NET webform. It works, but connection string to the database is hard coded, as in this method : public DataSet...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
22
by: Arne | last post by:
How do I pass a dataset to a webservices? I need to submit a shoppingcart from a pocket PC to a webservice. What is the right datatype? II have tried dataset as a datatype, but I can't get it to...
5
by: Darious Snell | last post by:
I am using windows forms and vb.net. My problem is a little complex so please bear with me. I have written an application that references a .com based API linked to an external client...
2
by: Carl Heller | last post by:
Working in VS2003, .Net 1.1 I'm working on a project where I compare data between two databases. This is a lengthy process, and very data intensive, so I decided to create a class, and thread...
0
by: Magnus Bergh | last post by:
I am developing an application for pocketpc and this involvs a but of juggling with different forms. I have an "order entry" type of application. On the main form I have a grid which displays...
0
by: John Sheppard | last post by:
Hello there, I am wanting to pass my datasets by reference to other forms. As soon as I do that I have to reset any binding sources by setting their datasource property to the original...
4
by: John Sheppard | last post by:
Hello there I was wondering if anyone could help me, I am trying to pass a typed dataset to a dialoged child form by reference. I have binding sources sitting on the child form. So to refresh...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.