10 Replies - 284 Views - Last Post: 30 April 2013 - 11:34 AM Rate Topic: -----

#1 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 80
  • Joined: 22-April 13

Column calling error

Posted 30 April 2013 - 10:56 AM

I've figured out how to iterate data out of a column of a DataTable. I have tried calling the specific column name for formatting. I'm having a problem when I start to run the compiler. It says "Cannot apply indexing with [] to an expression of type 'System.Data.DataColumn'". This is an error that I've never encountered this and I don't know how else I would index the specific column for formatting. This is for a formatting task. If anyone could guide me in the right direction, it would be greatly appreciated. I've looked around on MSDN's documentations and I've found nothing about this error or how to fix it. I've also looked around several other programming forums and still haven't found anything about this error. Here is the code snippet:
foreach (DataColumn records in data.Columns)
            { //Starts foreach
                string BN = records["BOOKINGID"].ToString();
            }
Thanks in advance for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Column calling error

#2 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Column calling error

Posted 30 April 2013 - 11:00 AM

have you read about indexingand what it does, on that MSDN that you looked?

This post has been edited by Michael26: 30 April 2013 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,846
  • Joined: 12-December 12

Re: Column calling error

Posted 30 April 2013 - 11:02 AM

If "BOOKINGID" is a column-name then I assume you should be navigating rows, not columns.

What are you attempting to achieve?
Was This Post Helpful? 0
  • +
  • -

#4 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 80
  • Joined: 22-April 13

Re: Column calling error

Posted 30 April 2013 - 11:08 AM

View PostMichael26, on 30 April 2013 - 11:00 AM, said:

have you read about indexing and what it does?

Yes. I used indexers in MySQL. I'm sort of new to C# so if it works just like MySQL, it should be selecting the column names with the string indexer. If it's different than MySQL, I have read it but it's still sort of new to me so I don't have a full grasp on it yet. I've been learning this on my own since the beginning of the year.

View Postandrewsw, on 30 April 2013 - 11:02 AM, said:

If "BOOKINGID" is a column-name then I assume you should be navigating rows, not columns.

What are you attempting to achieve?

Select all the data within a specific column and formatting it to a specific length in a text file.
Was This Post Helpful? 0
  • +
  • -

#5 Michael26  Icon User is online

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Column calling error

Posted 30 April 2013 - 11:10 AM

I don't think indexers in Mysql and indexers in C# are the same thing.
This is quote from MSDN

Quote

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

This post has been edited by Michael26: 30 April 2013 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 80
  • Joined: 22-April 13

Re: Column calling error

Posted 30 April 2013 - 11:18 AM

View PostMichael26, on 30 April 2013 - 11:10 AM, said:

I don't think indexers in Mysql and indexers in C# are the same thing.

Then I don't have a full grasp on the indexers of C#. I mean I've read about indexers but I don't have a full understand as I'm not very advanced in C#. I'm still confused though on indexers. It seems like it holds positions? Not completely sure though. It also says that it can use strings instead of integers on the page that I read which lead me to believe that it worked just the same way as MySQL indexers worked.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,846
  • Joined: 12-December 12

Re: Column calling error

Posted 30 April 2013 - 11:20 AM

I assume you want something like:

foreach(DataRow drow in data.Rows)
{
    string BN = drow["BOOKINGID"].ToString();
}

This post has been edited by andrewsw: 30 April 2013 - 11:21 AM

Was This Post Helpful? 1
  • +
  • -

#8 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 80
  • Joined: 22-April 13

Re: Column calling error

Posted 30 April 2013 - 11:23 AM

View PostMichael26, on 30 April 2013 - 11:10 AM, said:

I don't think indexers in Mysql and indexers in C# are the same thing.
This is quote from MSDN

Quote

Indexers allow instances of a class or struct to be indexed just like arrays. Indexers resemble properties except that their accessors take parameters.

The page is here.

View Postandrewsw, on 30 April 2013 - 11:20 AM, said:

I assume you want something like:

foreach(DataRow drow in data.Rows)
{
    string BN = drow["BOOKINGID"].ToString();
}

Yes. Something like that. Would that fetch all the data in that column?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,846
  • Joined: 12-December 12

Re: Column calling error

Posted 30 April 2013 - 11:27 AM

drow["BOOKINGID"] is indexing the DataRow, as would drow[0] be, but you cannot index a DataColumn in the same way.

View Postdak45309, on 30 April 2013 - 06:23 PM, said:

foreach(DataRow drow in data.Rows)
{
    string BN = drow["BOOKINGID"].ToString();
}

Yes. Something like that. Would that fetch all the data in that column?
[/quote]
Yes it would do, although I haven't run it. Try it!

But, of course, if you just have the assignment to BN in the loop then it will only end up storing the last row's value (and you should presumably declare the string outside the loop anyway).
Was This Post Helpful? 1
  • +
  • -

#10 dak45309  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 80
  • Joined: 22-April 13

Re: Column calling error

Posted 30 April 2013 - 11:29 AM

View Postandrewsw, on 30 April 2013 - 11:27 AM, said:

drow["BOOKINGID"] is indexing the DataRow, as would drow[0] be, but you cannot index a DataColumn in the same way.

View Postdak45309, on 30 April 2013 - 06:23 PM, said:

foreach(DataRow drow in data.Rows)
{
    string BN = drow["BOOKINGID"].ToString();
}

Yes. Something like that. Would that fetch all the data in that column?

Yes it would do, although I haven't run it. Try it!

But, of course, if you just have the assignment to BN in the loop then it will only end up storing the last row's value (and you should presumably declare the string outside the loop anyway).
[/quote]
That's good coding practice. I would've done that anyways :) Thanks for the help. How do I mark this as resolved now?
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,846
  • Joined: 12-December 12

Re: Column calling error

Posted 30 April 2013 - 11:34 AM

Quote

Thanks for the help. How do I mark this as resolved now?


You don't need to. Good luck, Andy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1