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 2989
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 - 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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |