Solved

Looping through dataset rows conditionally outputting columns

Posted on 2009-03-30
2
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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