Solved

Looping through dataset rows conditionally outputting columns

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now