Solved

Looping through dataset rows conditionally outputting columns

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

19 Experts available now in Live!

Get 1:1 Help Now