How would I Loop though this DataSet?

I'm trying ot loop through this dataSet with a foreach loop.  I wouldl like to be able to generate/create buttons that when clicked select the item, or row of data, and put the value in a label. But I got stuck here.  Thanks for any help.


code:
// fill the DataSet with data form data adapter 
        myda.Fill(myds);

//close connection
        sqlConnection1.Close();

//  ds data into variables
        DataRow myRow = myds.Tables[0].Rows[0];
        string cID = myRow[myds.Tables[0].Columns["courseID"]].ToString();
        string n = myRow[myds.Tables[0].Columns["CourseName"]].ToString();
        string d = myRow[myds.Tables[0].Columns["Description"]].ToString();
        string c = myRow[myds.Tables[0].Columns["Cost"]].ToString();
       
        Console.WriteLine(cID.ToString());

Open in new window

bmanmike39Asked:
Who is Participating?
 
käµfm³d 👽Commented:
You could do:

foreach (DataRow row in myds.Tables[0].Rows)
{
    string cID = myRow["courseID"].ToString();
    string n = myRow["CourseName"].ToString();
    string d = myRow["Description"].ToString();
    string c = myRow["Cost"].ToString();
       
    Console.WriteLine(cID.ToString());
}

Open in new window

0
 
bmanmike39Author Commented:
I set a break point and it loops though all the code but, i get no output on  the page.
0
 
käµfm³d 👽Commented:
You have to change line 8 (my post). I only duplicated what you already had. Right now, it's writing to the Console. You need to adjust it to go to the screen. You could start with a Response.Write call instead of Console.WriteLine if you just want to test that you can see output.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
bmanmike39Author Commented:
Thanks i changed it to response.write, but it's only returning the first row  in the db.  I used a break point and it show same value as it goes through the code for each loop.
0
 
käµfm³d 👽Commented:
My fault. Change one more thing:  change "myRow" to "row" (all occurrences within the foreach).
0
 
bmanmike39Author Commented:
Thanks!!!

It works well!  One more though,  I need to create a button for each record

I tried this butt can't get it to work. I added it to the end of each loop.

Button sbt = new Button();

Open in new window

0
 
käµfm³d 👽Commented:
You have to add the new button to the page:

Button sbt = new Button();
this.Controls.Add(sbt);

Open in new window

0
 
bmanmike39Author Commented:
Add this form1.Controls.Add(sbt);

but got this error:
Control 'ctl02' of type 'Button' must be placed inside a form tag with runat=server.
0
 
bmanmike39Author Commented:
I added a button inside the form tags, with the id sbt
then i added "this.Controls.Add(sbt);"

With or without the this.Controls.Add(sbt)  I get he same error
0
 
Obadiah ChristopherCommented:
Which method you are calling this code?

Tried setting the id of the button?

Please show us the aspx markup.
0
 
bmanmike39Author Commented:
Found that i needed a placholder, but the problem is it bring back all the records in order, in a row, then below that it displays all the buttons in a row. I'm still trying to figure out how to add the click event to each button and have the button display under each record, and not all together.

Thanks

This in a foreach loop:

Label cn = new Label();
Button sbt = new Button();

string cID = row["coursID"].ToString();
string n = row["CourseName"].ToString();
string d = row["Description"].ToString();
string c = row["Cost"].ToString();



cn.Text = c;  
sbt.Text = "Select Item";
PlaceHolder pl2 = new PlaceHolder();
PlaceHolder pl1 = new PlaceHolder();
PlaceHolder2.Controls.Add(cn);
PlaceHolder1.Controls.Add(sbt);

// Add a break
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
0
 
Obadiah ChristopherCommented:
From what I understand, you can use the GridView Control and use a TemplateField to add a button.
0
 
bmanmike39Author Commented:
For my project the gridview will not work.  I need to create labels and buttons on the fly when looping through the records.
0
 
bmanmike39Author Commented:
Don't know where to go from here.  the gridview control will not work as i said.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.