?
Solved

How would I Loop though this DataSet?

Posted on 2012-08-12
14
Medium Priority
?
599 Views
Last Modified: 2012-10-03
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

0
Comment
Question by:bmanmike39
  • 8
  • 4
  • 2
14 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38285551
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
 

Author Comment

by:bmanmike39
ID: 38285572
I set a break point and it loops though all the code but, i get no output on  the page.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38285634
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 

Author Comment

by:bmanmike39
ID: 38285673
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38286093
My fault. Change one more thing:  change "myRow" to "row" (all occurrences within the foreach).
0
 

Author Comment

by:bmanmike39
ID: 38286215
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38286226
You have to add the new button to the page:

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

Open in new window

0
 

Author Comment

by:bmanmike39
ID: 38286249
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
 

Author Comment

by:bmanmike39
ID: 38286278
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
 
LVL 20

Expert Comment

by:informaniac
ID: 38287336
Which method you are calling this code?

Tried setting the id of the button?

Please show us the aspx markup.
0
 

Author Comment

by:bmanmike39
ID: 38287475
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
 
LVL 20

Expert Comment

by:informaniac
ID: 38289067
From what I understand, you can use the GridView Control and use a TemplateField to add a button.
0
 

Author Comment

by:bmanmike39
ID: 38292472
For my project the gridview will not work.  I need to create labels and buttons on the fly when looping through the records.
0
 

Author Comment

by:bmanmike39
ID: 38364776
Don't know where to go from here.  the gridview control will not work as i said.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question