Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > ADO.NET
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2005, 03:00 PM
Registered User
 
Join Date: Sep 2005
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

 
Old September 16th, 2005, 03:19 AM
Registered User
 
Join Date: Sep 2005
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

 
Old September 28th, 2005, 06:43 PM
Registered User
 
Join Date: Dec 2004
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





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 05: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 04:14 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.