Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOR LOOP NESTED QUERY FOR DATABASE 4

Status
Not open for further replies.

UKLAD1980

Programmer
Dec 6, 2008
28
GB
Hi Guys

Really sorry to trouble you all but I could really use some help on this.



I am currently learning from a book about database programming in c#. I have loaded the data into my dataset etc.


just for example data set is.....


PersonName Age

John 34

James 23

Simon 45

Peter 15


Using the above example dataset how does the following code work step-by-step in each stage.

foreach (DataRow dRow in dt.Rows)

{

foreach (DataColumn datacol in dt.Columns)

Console.WriteLine(dRow[datacol]);

Console.WriteLine("================");

}




How does the loop work in each stage?? How is a for loop different to a foreach loop?

If I can see step by step then I will be able to understand.
Any help would be amazing. Sorry If this question is very simple for you professionals. I guess we all have to start from somewhere.

As I said I would appreciate as much help as possible.

Cheers


:)
 
A for loop loops a specified number of times. A foreach loop "repeats a group of embedded statements for each element in an array or an object collection" to quote MSDN. An example of an object collection would be the collection of row objects in the dt.Rows collection in your code, as well as the columns collection.

Now, you haven't said whether you're having problems with the code or just problems understanding how it works. If the latter, then what you're doing is going through each member of the rows collection, and once for each row, going through each member of the columns collection.

HTH

Bob
 
Hi Bob,

You really need to get some shuteye chief. Think of the heavy bags under your eyes :)

Many thanks for looking at this thread. No, you was right I have posted this up twice but under two different titles. Appreciate that this must be annoying so please accept my sincere apologiese.

Is it just me but would a beginner like myself not get confused by the msdn quote? Anyways let me try and explain again.

I understand what we are trying to in the section of the book and the processes we need to do and why but what I do not understand is how the code example I have provided works (what happens line by line(. Where does the loop end and where does it start? What data using the dataset is being picked up and at what stage?

I understand by running the code from the book we should get

get something like......

''

John
34
===========

James
23
===========

Simon
45
===========
Peter
15
===========

"

How is that achieved? Why is the PersonName above the Age when we have use a right line.

As you can see I am pretty new to all this but if you can show me step by step how the above list is constructed by the code it would be very very much appreciated. I was doing ok up until this point and would be shame to fail now:)

When you guys started to programme id dyou have the same easy problems or am I just stupid?

Thanks Bob and everyone

UK
 
In your example you have four rows and two columns.

What the code you have does is loop through each row
Code:
foreach (DataRow dRow in dt.Rows)
While it is on a particular row it loops through each of the columns in that row
Code:
foreach (DataColumn datacol in dt.Columns)
For every column it loops through it writes the value of that column to a new line in the console
Code:
Console.WriteLine(dRow[datacol]);
Once it's looped each column it writes a new seperator line to the console
Code:
Console.WriteLine("================");
Hope this helps explain things a bit for you.

Cheers

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Sorry, the last part of that isn't as clear as I'd have liked. It would have been more helpful if it read:

Once it's looped through each of the columns for that row it writes a new seperator line to the console
Code:
Console.WriteLine("================");
Hope this is a bit clearer.

Cheers

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I'll try again, in my last post by each I did of course mean all.

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Harley,

You are an absolute living legend. I salute you. Do you want to move in with me and make me a programming genius? :)

Would it possible for you to use the example data to constuct a snippet of what is displayed at each stage so tat I can truly understand.

If I am honest I'm not grasping the Console.WriteLine(dRow[datacol]); part of the loop. I guess it ends when there is no more rows or it returns a false value.

Is it ok to do a step by step for me? Am I stupid or is it normal to ask such questions when first staring out?? I understand many parts of programming but it is puting thm toegther.

Many thanks again:)
 
Honestly, no question is stupid when you're first starting programming (plus I'm fairly new to the C# flavour of programming myself so I might ask someone else a fairly stupid question at some point [wink])! [smile]

I'll give you an example of the first iteration of the loop that should help clear things up a bit:

Your first row contains the data
Code:
John             34
The first column contains "John", and the second column contains "34".

Your code loops each row (so imagine we're now on this row) and then for each particular row it encounters it then loops through each of the columns and outputs the column value for the row it's on (demonstrated by the code)
Code:
Console.WriteLine(dRow[datacol]);
So, first time through the
Code:
foreach (DataColumn datacol in dt.Columns)
It will be John, so it writes John to the console window and then moves onto the next line in the console. This is because .WriteLine (from the helpfile) "Writes the specified data, followed by the current line terminator". Once that has been written the loop moves to the next column and writes that (which in this case would be "34") followed by a line terminator.

There are now no more columns of data in that particular row so it moves on to the next line of code (which writes the "================" and a line terminator.

It then moves onto the next row and repeats the process until there are no more rows (in this case, once it has written the fourth row).

If you put a breakpoint on the line
Code:
foreach (DataRow dRow in dt.Rows)
and step through it from there it should provide you with an insight into what's happening while allowing you to switch to check the console window to see what's being output. You can also mouse over the values at any time in the step through and see what will be used or what the current value is.

Hope this helps

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Harley you are an absolute Gem. That is perfect!!! I was being a complete fool.

Maybe one day I can help you one day with a query if I get good enough. Not sure if it would be the correct answer to your question thought :) Having said that I maybe working on the next Microsoft operating system or Facebook so you will have to wait hahaha.

Seriously thank you very much. Appreciate it.

Where abouts are you from?




 
Glad I could help, thanks for the star [smile]

I'm a North East lad but I'm in Sheffield at the minute mate.

Seems this is a more technical forum anything but technical discussion is deemed off topic, it might be worth joining forum1091 and continuing this conversation.

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
No worries on the star son, you deserve it and it is the least I can do.

What is the best way to get in contact?

 
I'm being a tool. Sorry. Have your real name dude. Will email you or something in a sec mate. No need to reply.

Cheers
 
<Think of the heavy bags under your eyes

Why should I do that? Makes me feel old. [lol]
 
<I'm fairly new to the C# flavour of programming myself

I'm newer than you are. :p
 
UKLad, you can teach yourself a great deal by learning how to step through your code, and also how to print out variables using the watch window and immediate window. That's pretty much how I learned both Paradox (for DOS!) and VB.

Your questions are now much more specific, and therefore much easier to answer. Thanks for following through on the given advice. Also, I do understand that feeling of frustration that comes with not completely understanding what's going on. If you adopt a stance of relentless tenacity, as you appear to have done, you'll do fine.

Now, this:

Why is the PersonName above the Age when we have use a right line.
I assume that in this quote the term "right line" refers to "WriteLine." That said, WriteLine writes an entire line as well as a new line character (yes, a new line is a "character", just as tab and backspace are). So, every time you call the WriteLine method, it will put whatever you want to write as its own line, and start the next on a new one. If you don't want this behavior, use the Write method instead.

By the way, if you want to see the list of non-character keys that are supported as characters, google "ascii tables" and observe what you find.
 
Bob,

Thanks for getting back to me big guy. Regarding the 'heavy bags' I was refering to your late 1am finish last night:)

I am learning 'pure' c# as well as database c# as I go along. For the 'pure' C# I am using a book called 'headfirst c#' which is pretty good. I do tend to use breakpoints in my code but for some reason when using it for the above loop the cosole just pops up and nothing else happens? What am I doing wrong mate?

Cheers
 
Bob said:
I'm newer than you are. [tongue]
How new Bob? I think I've been doing it for three weeks now [wink]

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
The reason the console just pops up and then disappears is that as it's not expecting any user input it finishes the code and then closes the console. If you try this code it will keep the window open until you either press return or manually close it.
Code:
foreach (DataRow dRow in dt.Rows)

{

foreach (DataColumn datacol in dt.Columns)

Console.WriteLine(dRow[datacol]);

Console.WriteLine("================");

}

Console.ReadLine();
Hope this helps



HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
There is no way you two are novices. I would say errr, seasoned professionals. Cheers for that that Harley. The stupid thing is I have been using the Readline(); method all the way through learning but for some some strange reason I just didn't pick it up at this point.

On a another note dude, did you get my private message on your little forum you are part of. I also sent you (well think it was you) a message on facebook.

Thanks again mate.
 
C# novices admittedly, I imagine between us there's a few years of programming in other languages stuffed away somewhere in there [tongue]

UKLAD1980 said:
seasoned professionals
I can't speak for Bob but that's the first time I've ever been called that [wink]

On the other note, my work filter is blocking the other website temporarily (and Facebook permenantly) at the minute so until I get my home connection set up (not been in there that long) I won't be able to get them I'm afraid mate [sad]

HarleyQuinn
---------------------------------
You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top