Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Looping through dataset rows conditionally outputting columns

Posted on 2009-03-30
2
Medium Priority
?
243 Views
Last Modified: 2012-05-06
Hi All,

I have a dataset containing a similar table to the 1 below:

ProductTypeID      Title     Colour
1                            Shirt    Blue
2                            Lace    NULL
1                            Shirt    Blue

Please could anyone show me how to iterate through each row in the dataset writing it to the browser using response.write() but if the ProductTypeID isn't 1 then omit the Colour column?

Many thanks,

Rit
0
Comment
Question by:rito1
2 Comments
 
LVL 27

Accepted Solution

by:
nmarun earned 2000 total points
ID: 24020455
There are different ways to do this. Here's one:

string sqlConnection = "define connection string here";
//http://www.connectionstrings.com/
 
using (SqlConnection sqlConn = new SqlConnection(sqlConnection))
{
    SqlCommand cmd = new SqlCommand("Select ProductTypeID, Title, Colour from myTable", sqlConn);
    SqlDataReader rdr = cmd.ExecuteReader();
    Response.Write("<table><tr><th>ID</th><th>Title</th><th>Colour</th></tr>");
    while(rdr.Read())
    {
        if(rdr["ProductTypeID"].ToString() == "1")
        {
            Response.Write("<tr><td>" + rdr["ProductTypeID"] + "</td><td>" + rdr["Title"] + "</td><td></td></tr>");
        }
        else
        {
            Response.Write("<tr><td>" + rdr["ProductTypeID"] + "</td><td>" + rdr["Title"] + "</td><td>" + rdr["Colour"] + "</td></tr>");
        }
    }
}

Open in new window

0
 
LVL 1

Author Closing Comment

by:rito1
ID: 31564394
Excellent. This suits what I am doing. Thank You
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 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