Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / June 2007

Tip: Looking for answers? Try searching our database.

filling a data table form a sqldata reader

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bbdobuddy - 28 Jun 2007 17:54 GMT
Hi,

I am trying to populate a datatable from a sql data reader but I am having
some issues that hopefully somebody can help me out with.

I have added the using System.Data.Common to the top of my form

This is my code
               connect = new
System.Data.SqlClient.SqlConnection(connectString);
               command = new System.Data.SqlClient.SqlCommand("SELECT *
FROM "+owner+tableName+ " WHERE "+ tableField+ " IN("+inClause+")",connect);
               connect.Open();
               reader = command.ExecuteReader();

               System.Data.Common.DataAdapter da;
               da.Fill(dataTbl, reader);

And the error i get is

System.Data.Common.DataAdapter.Fill(System.data.DataTable,System.Data.IDataReader) is inaccessible due to its protection level.

I believe that I need to probably add a reference but I am not sure what to
add.

Any help would be appreciated.

Thanks
Simon Hart - 28 Jun 2007 18:52 GMT
Your syntax is incorrect - you need to read up on ADO.NET.

If you use a DataReader which is forward only, you generally populate your
own business object (not datasets) as you have to read through the database
sequentially.

You are getting the DataSet and DataReader mixed up. When you use a
DataAdapter, you generally use DataSets. When you use the Fill method, you
fill a DataSet object, not a DataReader.

When you create the DataAdapter you need to assign it a SqlConnection
object, your code isn't doing this.

Another tip, using a DataAdapter you don't need to open and close the
connection, the DataAdapter does this for you automatically. If using a
DataReader - always remember to close it afterwards.

So try changing your code to something like:

                connect = new
                System.Data.SqlClient.SqlConnection(connectString);
               

                System.Data.Common.DataAdapter da = new
System.Data.Common.DataAdapter("SELECT *
FROM "+owner+tableName+ " WHERE "+ tableField+ " IN("+inClause+")",connect);
        DataSet mySet = new DataSet("Myset");

                da.Fill(mySet);

I'd recommend using the Data Access Block from the Client Software Factory,
it makes database coding so much easier. I had to scratch my head to answer
this post as I can bearly remember ADO now that I use the data application
block.
Signature

Simon Hart
http://simonrhart.blogspot.com

> Hi,
>
[quoted text clipped - 24 lines]
>
> Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.