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.
|
|
|