Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ADO.NET For discussion about ADO.NET.††Topics such as question regarding the System.Data namespace are appropriate.††Questions specific to a particular application should be posted in a forum specific to the application .
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ADO.NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 15th, 2005, 03:00 PM
Registered User
 
Join Date: Sep 2005
Location: Bangalore, Karnataka, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting Error in foreach loop for DataSet

Hi,

The following code sample is from the Book "Professional ADO.NET with VB.NET (Wrox)". I am getting the following error for the foreach loop. The error is "'System.Data.DataSet.Tables' denotes a 'property' where a 'method' was expected"

The Code is:


using System;
using System.Data;
using System.Data.SqlClient;
namespace PublicTest
{
    class myClass
    {
        [STAThread]
        static void Main(string[] args)
        {
            string strCon = "Server=LOGICON-SERVER1,1433;Database=pubs;integrated security=SSPI;persist security info=False;Connection Timeout=10000;";
            string strSQL = "Select au_fname, au_lname, phone from authors where city = 'Oakland'";

            SqlConnection myCon = new SqlConnection(strCon);
            SqlCommand myCom = new SqlCommand(strSQL, myCon);

            SqlDataAdapter da = new SqlDataAdapter(myCom);
            DataSet ds = new DataSet();
            da.Fill(ds, "authors");
            Console.WriteLine("gjjhhj", ds.Tables(0).ToString());
            foreach (DataColumn dc in ds.Tables
            {
                Console.Write("{0,15}", dc.ColumnName);
            }
            Console.ReadLine();
        }
    }
}


Please help me.


Thanks and Regards,
Ravi

Reply With Quote
  #2 (permalink)  
Old September 16th, 2005, 03:19 AM
Registered User
 
Join Date: Sep 2005
Location: Bangalore, Karnataka, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi all,

There was a mistake in foreach loop. I am again posting my code. Pleasae help me.

==============================
using System;
using System.Data;
using System.Data.SqlClient;
namespace PublicTest
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class myClass
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            string strCon = "Server=LOGICON-SERVER1,1433;Database=pubs;integrated security=SSPI;persist security info=False;Connection Timeout=10000;";
            string strSQL = "Select au_fname, au_lname, phone from authors where city = 'Oakland'";

            SqlConnection myCon = new SqlConnection(strCon);
            SqlCommand myCom = new SqlCommand(strSQL, myCon);

            SqlDataAdapter da = new SqlDataAdapter(myCom);
            DataSet ds = new DataSet();
            da.Fill(ds, "authors");

            foreach (DataColumn dc in ds.Tables(0).Column)
            {
                Console.Write("{0,15}", dc.ColumnName);
            }

        }
    }
}

====================

Thanks & Regards,
Ravi

Reply With Quote
  #3 (permalink)  
Old September 28th, 2005, 06:43 PM
Registered User
 
Join Date: Dec 2004
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this code

 foreach(DataTable myTable in myDataSet.Tables){
       foreach(DataColumn myColumn in myTable.Columns){
          Console.WriteLine(myColumn.ColumnName);
       }
    }


Also see this link

http://msdn.microsoft.com/library/de...nnametopic.asp
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ch. 4 - Foreach loop container Rhywun BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 1 May 5th, 2007 08:31 AM
Does foreach loop support datareader deb_kareng C# 1 February 23rd, 2007 04:30 AM
ForEach Loop Bushido121 BOOK: Professional SQL Server 2005 Integration Services ISBN: 0-7645-8435-9 0 October 23rd, 2006 07:55 PM
Need help with foreach loop Arsi C# 3 September 7th, 2004 09:41 PM
foreach loop not working rajanikrishna Beginning PHP 1 November 3rd, 2003 03:14 AM



All times are GMT -4. The time now is 11:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.