473,670 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why exception when going through the DataSet?

When executing the following code

ArrayList list = new ArrayList();
try
{
//Make sure connection is open
if (!connection.St ate.ToString(). Equals("Open"))
connection.Open ();

DataSet list = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "select " + m_TableSerieCol Name + "
from " + m_TableSerie, connection);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----
list.Add((strin g) dr[m_TableSerieCol Name]);
}
catch (SqlException exc)
{
MessageBox.Show (exc.Message);
return;
}

I get the following exception:
---
An unhandled exception of type 'System.NullRef erenceException ' occurred in
SportsResult.ex e

Additional information: Object reference not set to an instance of an object.
---
on the line

foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----

Rows has a length of 0, so what I expect is the foreach loop to execute 0
times, but instead I then get the exception. Why?

Nov 17 '05 #1
5 1570
It isn't exactly as I wrote. This is the code:

ArrayList list = new ArrayList();
DataSet ds = SelectQuery("se lect " + m_TableSerieCol Name + " from " +
m_TableSerie);
foreach (DataRow dr in ds.Tables[m_TableSerie].Rows)
list.Add((strin g) dr[m_TableSerieCol Name]);
return list;

public DataSet SelectQuery(str ing query)
{
try
{
//Make sure connection is open
if (!connection.St ate.ToString(). Equals("Open"))
connection.Open ();

SqlDataAdapter da = new SqlDataAdapter( query, connection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (SqlException exc)
{
MessageBox.Show (exc.Message);
return new DataSet();
}
}

Note that, although ds is local to the SelectQuery function, it has a
Count=1 when returned. What could be wrong?
"Joachim" wrote:
When executing the following code

ArrayList list = new ArrayList();
try
{
//Make sure connection is open
if (!connection.St ate.ToString(). Equals("Open"))
connection.Open ();

DataSet list = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "select " + m_TableSerieCol Name + "
from " + m_TableSerie, connection);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----
list.Add((strin g) dr[m_TableSerieCol Name]);
}
catch (SqlException exc)
{
MessageBox.Show (exc.Message);
return;
}

I get the following exception:
---
An unhandled exception of type 'System.NullRef erenceException ' occurred in
SportsResult.ex e

Additional information: Object reference not set to an instance of an object.
---
on the line

foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----

Rows has a length of 0, so what I expect is the foreach loop to execute 0
times, but instead I then get the exception. Why?

Nov 17 '05 #2
On Mon, 28 Mar 2005 03:57:02 -0800, "Joachim"
<Jo*****@discus sions.microsoft .com> wrote:
When executing the following code

ArrayList list = new ArrayList();
try
{
//Make sure connection is open
if (!connection.St ate.ToString(). Equals("Open"))
connection.Open ();

DataSet list = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "select " + m_TableSerieCol Name + "
from " + m_TableSerie, connection);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----
list.Add((strin g) dr[m_TableSerieCol Name]);
}
catch (SqlException exc)
{
MessageBox.Show (exc.Message);
return;
}

I get the following exception:
---
An unhandled exception of type 'System.NullRef erenceException ' occurred in
SportsResult.e xe

Additional information: Object reference not set to an instance of an object.
---
on the line

foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----

Rows has a length of 0, so what I expect is the foreach loop to execute 0
times, but instead I then get the exception. Why?


Are you sure that ds.Tables[m_TableSerie] is not null?

--
Ludwig Stuyck
http://www.coders-lab.net
Nov 17 '05 #3
Yes.

I managed to solve the problem. If I instead used DataTable instead of
DataSet , and made som additional changes, it worked. The problem seemed to
be that the DataSet didn't name the table properly (by default the table was
called "Table").

Thanks

"Ludwig Stuyck" wrote:
On Mon, 28 Mar 2005 03:57:02 -0800, "Joachim"
<Jo*****@discus sions.microsoft .com> wrote:
When executing the following code

ArrayList list = new ArrayList();
try
{
//Make sure connection is open
if (!connection.St ate.ToString(). Equals("Open"))
connection.Open ();

DataSet list = new DataSet();
SqlDataAdapter da = new SqlDataAdapter( "select " + m_TableSerieCol Name + "
from " + m_TableSerie, connection);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----
list.Add((strin g) dr[m_TableSerieCol Name]);
}
catch (SqlException exc)
{
MessageBox.Show (exc.Message);
return;
}

I get the following exception:
---
An unhandled exception of type 'System.NullRef erenceException ' occurred in
SportsResult.e xe

Additional information: Object reference not set to an instance of an object.
---
on the line

foreach (DataRow dr in ds.Tables[m_TableSerie].Rows) //<-----

Rows has a length of 0, so what I expect is the foreach loop to execute 0
times, but instead I then get the exception. Why?


Are you sure that ds.Tables[m_TableSerie] is not null?

--
Ludwig Stuyck
http://www.coders-lab.net

Nov 17 '05 #4
"Joachim" <Jo*****@discus sions.microsoft .com> wrote in message
news:34******** *************** ***********@mic rosoft.com...
I managed to solve the problem. If I instead used DataTable instead of
DataSet , and made som additional changes, it worked. The problem seemed to be that the DataSet didn't name the table properly (by default the table was called "Table").


True, it does not take the name from the underlying SQL statement.

Note that in your particular case, you are requesting only one column from
that table. To call that result set by the name of the database table would
be wrong, as it does not represent the table (which presumably has more
columns).

Further, in many case the result set is created by a join of two or more
different tables. If it were to base the DS TableName on such a query,
which table name should it use?

In the end, to have the DS TableName match the name of the table used in the
query would require the framework do a complex parsing the SQL statement,
only to determine that in most cases, it would have to use the default
anyway.
Nov 17 '05 #5
>I managed to solve the problem. If I instead used DataTable instead of
DataSet , and made som additional changes, it worked. The problem seemed to
be that the DataSet didn't name the table properly (by default the table was
called "Table").
Yes, that's the documented and expected behaviour - if you want to
change it, you'll have to NAME the table inside the data set in the
".Fill" call:
DataSet ds = new DataSet();
da.Fill(ds, m_TableSerie); <<== ADD THE NAME FOR THE RESULTING TABLE !!


If you don't specify a name for the table(s), the first one will be
called "Table", the second one "Table1", the third one "Table2" and so
on.

Marc
=============== =============== =============== =============== ====
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3896
by: James Ankrom | last post by:
Why does this fail? Dim relResources As New Data.DataRelation("Application_Resources", ..Tables("User_Applications").Columns("Application_id"), ..Tables("Resource_Rights").Columns("Application_id"), False) mUserData.Relations.Add(relResources) relResources.Nested = True Dim UserDataXML As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument(mUserData)
1
8586
by: Fleckman | last post by:
I have a situation where I need to add rows to tables with a Parent-Child relationship which presents a constraints violation when I reject the changes. Here is the scenario: I add a row to a Parent table which then causes a row to be added to the Child table referencing the parent row. The Child table rows can be modified by multiple processes so I need to manage row additionals separate from row modifications in terms of how they are...
4
1437
by: Frank Rizzo | last post by:
Hello. Every now and then (it's rare), i get an exception when calling SqlDataAdapter.Fill. The code is calling a stored procedure and attempts to stuff the results into a dataset. It yields the following exception. I am trying to understand what the exception is actually complaining about. Is it a problem with the data that came back or is it an issue inside the stored procedure? Any alternative ways to nail down this issue would...
3
2919
by: I am Sam | last post by:
I keep getting the following error message when I try to iterate through a CheckBoxList control: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance...
2
6076
by: Stanav | last post by:
Hello all, I'm developing a web application using VB.Net 2003 and Framework 1.1. This application queries an AS/400 database. I'm using the IBM OleDb provider that came with IBM Client Access for Windows (V5R3). Everything works fine on my development PC, but when I move the application to a Windows Server 2003, it crashes when trying to fill a dataset. I've double-checked that the Win 2k3 server does have Client Access installed, that it...
7
9528
by: Alan Pretre | last post by:
I have an application installed at a customer site that has been getting a general network error for a couple of years. I was hoping that .NET 2.0 would clear it up, but unfortunately it didn't. The .NET 2.0 exception does give a little more info than .NET 1.1 did, but not much. Can anybody give any pointers about how to track this down? It occurs every 10 hrs around the clock, like clockwork. I have included the stack trace, but...
1
1968
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm having difficulties figuring out the best way to handle (catch) exceptions in the different layers and then propagating those errors back up through the call stack to ultimately display something to the end-user. Note this is an intranet...
3
13927
by: =?Utf-8?B?SGVtaWw=?= | last post by:
Hi, I have written a web service for accessing data from a database. I have a method in the webservice which returns a dataset. I am trying to implement error handling by using the try...catch...finally structure. Now, the method's logic in the try block returns a dataset while the the code in the 'catch' block throws an exception object of the SoapException class. Before, returning an exception to the calling code, I build up an...
2
2938
ssnaik84
by: ssnaik84 | last post by:
Hello, I have hosted a website on GoDaddy.com. It's a share hosting. I am using XML file as a database. That means, I am reading and writing data into XML file instead of regular RDBMS (MySQL, MS SQL). I am reading XML data into DataSet and manipulate it and save the changes again to XML file. It works fine on my local machine.. but not working on GoDaddy server.. I am getting security exception.. Description: The application...
0
8466
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
8384
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
8901
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
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
7412
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
6212
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
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
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 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.