473,715 Members | 6,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing dataset between ASP.NET Web Forms

9 New Member
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 3005
nateraaaa
663 Recognized Expert Contributor
Second.aspx.cs

Code: ( text )
using System;
using System.Data;
using System.Data.Sql Client;
using System.Configur ation;
using System.Web;
using System.Web.Secu rity;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.W ebControls.WebP arts;
using System.Web.UI.H tmlControls;
using FirstPage;

public partial class Second : System.Web.UI.P age
{
protected void Page_Load(objec t sender, EventArgs e)
{
DataSet ds = (DataSet)Sessio n["myds"];
FillTable(ds);
GridView1.DataS ource = ds.Tables["Index"];
GridView1.DataB ind();
}

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

DataColumn indexID =
indexTable.Colu mns.Add("State" , typeof(string)) ;
indexTable.Colu mns.Add("Divisi on", typeof(string)) ;
indexTable.Colu mns.Add("MOIST_ INDEX", typeof(string)) ;
indexTable.Colu mns.Add("DROUGH T_INDEX", typeof(string)) ;
}
}
I believe what is happening is table Index does not exist in your dataset so GridView1.DataS ource = 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
zhshqzyc
9 New Member
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
1616
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 how i can use this filled dataset in the other forms, or i want to use user name in the second form which was entered at login form
3
13589
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 I create the procedure to pass by ref? Which means I don't need to add ByRef because it is defaulted by ref, correct? And in the constructor of the win form that gets created. I need to add in parameters a DataSet and SqlDataAdapter correct? What...
19
2531
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 GetCategories() { SqlConnection conn = new SqlConnection("Data Source=DEVSERVER;Initial Catalog=XXXX;User ID=X;Password=Y");
0
1486
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 into a background thread and get other data out and also update the main thread on which the main form was created. It seems to work fine. The basic function of the app is cheesy, I didn't spend any time on exception handling. northwind.mdb...
22
25586
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 compile. <WebMethod()> _ Public Function VerifySku(ByVal skus As XmlDataDocument) As DataSet Test program : Dim cartSet As DataSet cartSet = ws.VerifySku(cartSet)
5
1187
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 application. The API exposes client user interface objects. One of the objects is similar to a grid. The first part of my application creates a reference to the grid object in the client application.
2
2594
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 out the work. The order of work is as follows: 1. Retrieve the data from primary data source 2. Update UI with retrieved data - this is accomplished by passing a dataset as an event parameter
0
1544
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 Order headers. Let call this form "OrderList" From this view I edit/enter new orders by opening a new form for entering data. This is done using (more or less) the designer generated forms, so I have a "Order edit view dialog. I pass the binding...
0
843
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 datasource. I also find that I cannot then bind directly to my dataset without a binding source, so I have to duplicated all my binding sources on my second form. Is this normal or am I doing something wrong? How do you guys normally pass
4
5926
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 them I just set their datasource. I am guessing this is probably what is causing the problem. Is there a better way to do this? Anyway this all works happily and things show up when the record already exists but I have 2 problems ; 1) When I add...
0
8823
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8718
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9198
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...
0
9047
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
7973
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
6646
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
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3175
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
3
2119
bsmnconsultancy
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.