473,653 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Single element in a DataSet

OK, I have filled a DataSet with an SqlDataAdapter and I just want the data
in one column of one row. How can I do this since the following gives me
the infamous 'Object reference not set to an instance of an object' error:
strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
My table is called Users and I want the first row, and the only column is
also called Users.
I broke it down by declaring a DataTable and a DataRow etc and the problem
comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with no
problem, but cannot assign
rw=tbl.Rows(0) without getting the error above.
And you can't declare a New DataRow because the New constructor is Private

So can someone please give me example code on how to extract one element
from a DataSet into a variable so it can be used in code. MSDN only gives
examples on how to manipulate data while still in the DataSet or how to bind
it, rarely how to extract it in code.
The one example they do give uses
For Each tbl In datset.Tables
For Each rw In tbl.Rows
For Each col In tbl.Columns
Debug.Print(rw( col))
Next
Next
Next
But I still get the error when I try doing this, in the For Each rw In
tbl.Rows. Somehow I can't get any rows at all.
Nov 20 '05 #1
9 1686
The only thing I can think of is weather you did a fill..??

OleDbDataAdapte r1.Fill(DataSet 1)

and is your DataSet or DataSet1?

Also if you add the OleDataAdapter to your form you can do a preview data to
see what should be coming in... so the SQLDataAdapter should be the same..
does your data look correct at that point?

Hope this helps..
Ronald Walker

"Stephen" <gr******@bella tlantic.net> wrote in message
news:uz******** *****@TK2MSFTNG P11.phx.gbl...
OK, I have filled a DataSet with an SqlDataAdapter and I just want the data in one column of one row. How can I do this since the following gives me
the infamous 'Object reference not set to an instance of an object' error:
strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
My table is called Users and I want the first row, and the only column is
also called Users.
I broke it down by declaring a DataTable and a DataRow etc and the problem
comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with no problem, but cannot assign
rw=tbl.Rows(0) without getting the error above.
And you can't declare a New DataRow because the New constructor is Private

So can someone please give me example code on how to extract one element
from a DataSet into a variable so it can be used in code. MSDN only gives
examples on how to manipulate data while still in the DataSet or how to bind it, rarely how to extract it in code.
The one example they do give uses
For Each tbl In datset.Tables
For Each rw In tbl.Rows
For Each col In tbl.Columns
Debug.Print(rw( col))
Next
Next
Next
But I still get the error when I try doing this, in the For Each rw In
tbl.Rows. Somehow I can't get any rows at all.

Nov 20 '05 #2
Try

if datset.Tables.C ontains("Users" ) then
strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
end if

This will ensure that the table you want to use exists in the dataset.

Greetings
Daniel Walzenbach

"Ronald Walker" <rw@wow.com> schrieb im Newsbeitrag
news:ul******** ******@TK2MSFTN GP10.phx.gbl...
The only thing I can think of is weather you did a fill..??

OleDbDataAdapte r1.Fill(DataSet 1)

and is your DataSet or DataSet1?

Also if you add the OleDataAdapter to your form you can do a preview data to see what should be coming in... so the SQLDataAdapter should be the same..
does your data look correct at that point?

Hope this helps..
Ronald Walker

"Stephen" <gr******@bella tlantic.net> wrote in message
news:uz******** *****@TK2MSFTNG P11.phx.gbl...
OK, I have filled a DataSet with an SqlDataAdapter and I just want the

data
in one column of one row. How can I do this since the following gives me the infamous 'Object reference not set to an instance of an object' error: strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
My table is called Users and I want the first row, and the only column is also called Users.
I broke it down by declaring a DataTable and a DataRow etc and the problem comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with

no
problem, but cannot assign
rw=tbl.Rows(0) without getting the error above.
And you can't declare a New DataRow because the New constructor is Private
So can someone please give me example code on how to extract one element
from a DataSet into a variable so it can be used in code. MSDN only gives examples on how to manipulate data while still in the DataSet or how to

bind
it, rarely how to extract it in code.
The one example they do give uses
For Each tbl In datset.Tables
For Each rw In tbl.Rows
For Each col In tbl.Columns
Debug.Print(rw( col))
Next
Next
Next
But I still get the error when I try doing this, in the For Each rw In
tbl.Rows. Somehow I can't get any rows at all.


Nov 20 '05 #3
It does not sound like the query you used returned a rowset to be passed
into a DataTable by the Fill method. Let's see some code.

--
_______________ _______________ ______
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
_______________ _______________ ____

"Stephen" <gr******@bella tlantic.net> wrote in message
news:uz******** *****@TK2MSFTNG P11.phx.gbl...
OK, I have filled a DataSet with an SqlDataAdapter and I just want the data in one column of one row. How can I do this since the following gives me
the infamous 'Object reference not set to an instance of an object' error:
strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
My table is called Users and I want the first row, and the only column is
also called Users.
I broke it down by declaring a DataTable and a DataRow etc and the problem
comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with no problem, but cannot assign
rw=tbl.Rows(0) without getting the error above.
And you can't declare a New DataRow because the New constructor is Private

So can someone please give me example code on how to extract one element
from a DataSet into a variable so it can be used in code. MSDN only gives
examples on how to manipulate data while still in the DataSet or how to bind it, rarely how to extract it in code.
The one example they do give uses
For Each tbl In datset.Tables
For Each rw In tbl.Rows
For Each col In tbl.Columns
Debug.Print(rw( col))
Next
Next
Next
But I still get the error when I try doing this, in the For Each rw In
tbl.Rows. Somehow I can't get any rows at all.

Nov 20 '05 #4

Hi Stephen,

The 'Object reference not set to an instance of an object' error means that
your datset did not point to a suitable dataset that contains the data you
wanted.
You should check if your dataset have been filled correctly.
For example, you can fill your data set from the sqlserver like this:

DataSet ds=new DataSet();
SqlDataAdapter adapter=new SqlDataAdapter( "select * from
jobs","server=l ocalhost;databa se=pubs;uid=sa; pwd=");
adapter.Fill(ds );

After this, I think you can get the item in the DataSet correctly.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Stephen" <gr******@bella tlantic.net>
| Subject: Single element in a DataSet
| Date: Wed, 8 Oct 2003 17:42:49 -0400
| Lines: 28
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uz************ *@TK2MSFTNGP11. phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.framew ork.adonet,micr osoft.public.do tnet.languages. v
b,microsoft.pub lic.dotnet.lang uages.vb.data
| NNTP-Posting-Host: c-66-177-177-97.se.client2.a ttbi.com 66.177.177.97
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP11.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vb:145143
microsoft.publi c.dotnet.langua ges.vb.data:216 2
microsoft.publi c.dotnet.framew ork.adonet:6324 6
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vb
|
| OK, I have filled a DataSet with an SqlDataAdapter and I just want the
data
| in one column of one row. How can I do this since the following gives me
| the infamous 'Object reference not set to an instance of an object' error:
| strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
| My table is called Users and I want the first row, and the only column is
| also called Users.
| I broke it down by declaring a DataTable and a DataRow etc and the problem
| comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with
no
| problem, but cannot assign
| rw=tbl.Rows(0) without getting the error above.
| And you can't declare a New DataRow because the New constructor is Private
|
| So can someone please give me example code on how to extract one element
| from a DataSet into a variable so it can be used in code. MSDN only gives
| examples on how to manipulate data while still in the DataSet or how to
bind
| it, rarely how to extract it in code.
| The one example they do give uses
| For Each tbl In datset.Tables
| For Each rw In tbl.Rows
| For Each col In tbl.Columns
| Debug.Print(rw( col))
| Next
| Next
| Next
| But I still get the error when I try doing this, in the For Each rw In
| tbl.Rows. Somehow I can't get any rows at all.
|
|
|

Nov 20 '05 #5
Thanks for the suggestions guys. Turns out when you target a specific table
with an SqlAdapter, when it puts it into the DataSet it doesn't use the
original table name (I assume b/c there is only one), but instead changes
the Table.TableName to "Table" I'm now having no trouble with that part of
my code (but still pulling my hair out over some strange LoadControl errors,
look in aspnet newsgroup)

Stephen
Nov 20 '05 #6
"Stephen" <gr******@bella tlantic.net> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...
Thanks for the suggestions guys. Turns out when you target a specific table with an SqlAdapter, when it puts it into the DataSet it doesn't use the
original table name (I assume b/c there is only one), but instead changes
the Table.TableName to "Table" I'm now having no trouble with that part of my code (but still pulling my hair out over some strange LoadControl errors, look in aspnet newsgroup)


You can change which table is used in the DataSet by using the TableMappings
property of the DataAdapter.
--
John Saunders
Internet Engineer
jo***********@s urfcontrol.com
Nov 20 '05 #7
Is your Datatable really named "Users"? I bet it is named "Table", which is
the default. Look at TableMappings.
"Stephen" <gr******@bella tlantic.net> wrote in message
news:uz******** *****@TK2MSFTNG P11.phx.gbl...
OK, I have filled a DataSet with an SqlDataAdapter and I just want the data in one column of one row. How can I do this since the following gives me
the infamous 'Object reference not set to an instance of an object' error:
strng = datset.Tables(" Users").Rows(0) .Item("Users"). ToString()
My table is called Users and I want the first row, and the only column is
also called Users.
I broke it down by declaring a DataTable and a DataRow etc and the problem
comes up at the Rows stage, I can assign tbl=datset.Tabl es("Users") with no problem, but cannot assign
rw=tbl.Rows(0) without getting the error above.
And you can't declare a New DataRow because the New constructor is Private

So can someone please give me example code on how to extract one element
from a DataSet into a variable so it can be used in code. MSDN only gives
examples on how to manipulate data while still in the DataSet or how to bind it, rarely how to extract it in code.
The one example they do give uses
For Each tbl In datset.Tables
For Each rw In tbl.Rows
For Each col In tbl.Columns
Debug.Print(rw( col))
Next
Next
Next
But I still get the error when I try doing this, in the For Each rw In
tbl.Rows. Somehow I can't get any rows at all.

Nov 20 '05 #8
No, it REALLY IS called Users. I designed the table myself in the Server
Explorer. Trust me.

"Greg" <gr**@cds-am.net> wrote in message
news:OE******** ******@TK2MSFTN GP09.phx.gbl...
Is your Datatable really named "Users"? I bet it is named "Table", which is the default. Look at TableMappings.

Nov 20 '05 #9
You might have missed the point. When you use Fill, you can change the name
of the DataTable(s) which are used to refer to the rowset(s) returned by the
query. These are not set to the name of the root table in the server
automatically. If you choose "Fred",
ds = da.Fill("Fred")
the first DataTable is named Fred and the second Fred1 etc. You use the
TableMappings collection to tie the logical tables to the physical root
database tables so the Update method knows how to address the right table.

hth

--
_______________ _______________ ______
Bill Vaughn
MVP, hRD
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
_______________ _______________ ____

"Stephen" <gr******@bella tlantic.net> wrote in message
news:eV******** ******@tk2msftn gp13.phx.gbl...
No, it REALLY IS called Users. I designed the table myself in the Server
Explorer. Trust me.

"Greg" <gr**@cds-am.net> wrote in message
news:OE******** ******@TK2MSFTN GP09.phx.gbl...
Is your Datatable really named "Users"? I bet it is named "Table",
which is
the default. Look at TableMappings.


Nov 20 '05 #10

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

Similar topics

0
1374
by: magister | last post by:
Hello, I want to have a unique Quesiton Id key for each question under section. Not for every question in the Typed DataSet. Is there anyway I can do this...? Thanks, here is my typed Dataset XSD <?xml version="1.0" encoding="utf-8" ?>
0
1139
by: Smugsboy | last post by:
I have a XSD schema with a choice root element e.g.: <xs:element name="element1"> <xs:complexType> <xs:sequence> <xs:choice /> ... When I use the "generate dataset" option: I get a dataset with not tables within. How can I generate a dataset with table definitions corresponding to the elements inside the choice ?
5
8060
by: Paul Nations | last post by:
I'm in a real pickle. I've inherited a project with major problems. It receives encrypted XML files from a website not under my control (so I can't modify the web app) for insertion into a database. I'm under intense pressure to get the data into the database NOW. I can decrypt the files alright and they look like xml, they'll import into Access 2K3 with no problem, but VB.Net will not read them. It's complaining about no root...
2
7552
by: Json | last post by:
Ok, brand new to SQLXML 3.0 and its various issues. Heres what I'm trying to do: 1. I am trying to load xml data into an empty SQL table from my .NET console application. 2. This isn't a huge amount of data so I'm not interested in BulkXMLLoad (or whatever its called). 3. I have the xml and xml schema ready to go, with (I hope) the proper sql annotations.
3
1619
by: Prabhu Shastry | last post by:
Hello group, I have a Windows Service and an application (C#). Both processes need access to a single dataset object (created and modified by the service and application will only read the dataset). I thought this was possible with ..Net remoting, but not able to figure out a way to make this possible. Does anyone know if it's possible to achieve this? Thanks, -Prabhu
3
3510
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200 plus conference attendees). All web form datavalues will be one of the same 200 organizations in this list. I would like to avoid creating 50 separate exact copies of the same DataSet object. Can you help? Q. Exactly how do I use the same...
0
978
by: Simon Neve | last post by:
Hi, I have several datasets that use exactly the same element. From reading MSDN it appears I can split the common element into its own dataset and use the <xs: include element from any dataset that uses this common element. The datasets are returned from a web service. My question is this: when the web service client uses the datasets which include the common dataset, will this involve another round trip to the server to retrieve the...
1
1305
by: ronchese | last post by:
Hello All. I need to complement some information in a xml file, using the Xml.XmlDocument object. This xml contains several <xs:element ....> nodes (is a saved dataset), and I need to write new nodes exactly in the same way the existant nodes. I tried naming the new node as "xs:element" or type the "element" in the namespaceURI, but it when I see creates a namespace in the text file, in both cases. How can I do that?
4
5009
by: nautiboy | last post by:
This seems like it should be so trivial, but try as I might I can't figure it out. I want to write out a dataset as xml (i.e., DataSet.WriteXml()) but use the tablename as the root element instead of the datasetname. For example, if I have: private void simpleDataSetXml() { DataSet ds = new DataSet(); DataTable table = new DataTable("foo"); DataColumn col = new DataColumn("bar", System.Type.GetType("System.String"));...
0
8370
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
8283
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
8811
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...
0
8704
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...
1
8470
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
8590
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...
1
6160
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
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.