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. - System.NullReferenceException was unhandled by user code
-
Message="Object reference not set to an instance of an object."
First.aspx.cs - namespace FirstPage
-
{
-
public partial class GetStrings : System.Web.UI.Page
-
{
-
getStrings g = new getStrings();
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
-
SqlConnection myConnection = new SqlConnection(strConnectionString);
-
DataSet myDataSet = new DataSet();
-
try
-
{
-
myConnection.Open();
-
// create the data
-
GenerateDataSet(myDataSet, myConnection);
-
}
-
finally
-
{
-
myConnection.Close();
-
// bind each to table to a grid
-
-
}
-
Session["myds"] = myDataSet;
-
// databind the page
-
GridView1.DataSource = myDataSet.Tables["Weather"];
-
GridView1.DataBind();
-
}
and
Second.aspx.cs - 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));
-
}
-
}
Second.aspx - <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Second.aspx.cs" Inherits="Second" %>
-
<%@ Import Namespace="System.Data" %>
-
<%@ Import Namespace="System.Data.SqlClient" %>
-
<%@ Reference Page="First.aspx" %>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head id="Head1" runat="server">
-
<title>Untitled Page</title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<asp:GridView ID="GridView1" runat="server">
-
</asp:GridView>
-
</div>
-
</form>
-
</body>
-
</html>
Any help will be appreciated.
2 3005
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 - public void FillTable(out 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));
-
}
2. Change your method to return a DataSet and then add the DataTable to your DataSet. - public DataSet 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));
-
-
return dset;
-
}
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
DataSet ds = (DataSet)Session["myds"];
-
ds = FillTable(ds);
-
GridView1.DataSource = ds.Tables["Index"];
-
GridView1.DataBind();
-
}
-
Nathan
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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...
|
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");
|
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...
|
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)
| |
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.
|
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
|
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...
|
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
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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: 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...
| |