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


:)
 
Hey,

How would it know what the datasource is? In this case dataset1 (which I dragged from toolkit and filled using adapter) :)
 
because the datasource must implement IEnumerable. Whether that is a DataSet, DataTable, List<T>, Array, etc. doesn't matter.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I don't understand Jason.. You talking about the IListSource?

There is also a I collection and ItypedList....... and how the hell do you now this stuff off by heart? :)
 
... actually when typing in datagrid1 why am I not able to call the Setdatabindings method? or infact the DataBindings method?
 
no, get_data() is just a place holder for however you would get data. in your code above it's all part of the load event.
it may be in a separate member, or even a separate object.

looking at your code again, the problem my be that Form1_Load is private instead of public. This makes a difference with auto-wiring events.

here is simple refactoring on your code above
Code:
namespace MultiTableDataGrid
{
    public partial class Form1 : Form
    {
        [COLOR=blue]public[/color] void Form1_Load(object sender, EventArgs e)
        {
           dataGridView1.DataSource = get_data();
           dataGridView1.DataBind();
        }

        private IEnumerable get_data()
        {
            DataSet dataSet1 = new DataSet();
            using(SqlConnection sqlconnec = new SqlConnection(@"Server=localhost\SQLEXPRESS; Integrated Security=true; database=Northwind"))
            {
               SqlDataAdapter mydataadapter = new SqlDataAdapter("SELECT * FROM Employees;SELECT * FROM Orders;", connec);
               mydataadapter.TableMappings.Add("Table", "Employees");
               mydataadapter.TableMappings.Add("Table1", "Orders");

               mydataadapter.Fill(dataSet1);

               DataRelation mydatarelation = new DataRelation("EmployeeOrders", dataSet1.Tables[0].Columns["EmployeeID"], dataSet1.Tables[1].Columns["EmployeeID"]);

               dataSet1.Relations.Add(mydatarelation);
            }
            return dataSet1;
        }
    }
}
now with win forms development it may be slightly different, but webforms was designed to be executed like winforms for html, so it shouldn't be that much different.

passing IEnumerable instead of DataSet is the concept of programing to an abstraction instead of an implementation. this idea deserves a whole new thread of discussion.

and how the hell do you now this stuff off by heart?
8 hours a day. 5 days a week. + a few hours in between :) I also must give credit to my mentors, JP Boodhoo, Oren Eini (aka Ayende Rahien), and just about any blogger on LosTechies and CodeBetter and a slue of others.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Jason.... I have only gone and worked it out dude :)

It should read DataGridview1.Datasource = dataset1.tables["Employees"];


I have actually surprised myself
 
excellent!

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top