By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Single element in a DataSet

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.