Solved

Looping through dataset rows conditionally outputting columns

Posted on 2009-03-30
2
225 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 500 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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

831 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