473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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").ToStr ing()
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.Tables("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 1669
The only thing I can think of is weather you did a fill..??

OleDbDataAdapter1.Fill(DataSet1)

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******@bellatlantic.net> wrote in message
news:uz*************@TK2MSFTNGP11.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").ToStr ing()
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.Tables("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.Contains("Users") then
strng = datset.Tables("Users").Rows(0).Item("Users").ToStr ing()
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**************@TK2MSFTNGP10.phx.gbl...
The only thing I can think of is weather you did a fill..??

OleDbDataAdapter1.Fill(DataSet1)

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******@bellatlantic.net> wrote in message
news:uz*************@TK2MSFTNGP11.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").ToStr ing()
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.Tables("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******@bellatlantic.net> wrote in message
news:uz*************@TK2MSFTNGP11.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").ToStr ing()
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.Tables("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=localhost;database=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******@bellatlantic.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.public.dotnet.framework.adonet,microsoft .public.dotnet.languages.v
b,microsoft.public.dotnet.languages.vb.data
| NNTP-Posting-Host: c-66-177-177-97.se.client2.attbi.com 66.177.177.97
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:145143
microsoft.public.dotnet.languages.vb.data:2162
microsoft.public.dotnet.framework.adonet:63246
| X-Tomcat-NG: microsoft.public.dotnet.languages.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").ToStr ing()
| 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.Tables("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******@bellatlantic.net> wrote in message
news:ef**************@tk2msftngp13.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***********@surfcontrol.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******@bellatlantic.net> wrote in message
news:uz*************@TK2MSFTNGP11.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").ToStr ing()
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.Tables("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**************@TK2MSFTNGP09.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******@bellatlantic.net> wrote in message
news:eV**************@tk2msftngp13.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**************@TK2MSFTNGP09.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
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...
0
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...
5
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...
2
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...
3
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...
3
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...
0
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...
1
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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,...
0
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,...
0
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...
0
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...

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.