Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying recordset column names 1

Status
Not open for further replies.

snowneil

Programmer
Mar 22, 2006
40
GB
Hi,

Is there a way to display the column names of a recordset in asp javascript?

I have a recordset that is being used for reporting purposes and what is returned in sql is exactly what i want to have displayed.

An example of what is returned:
Code:
complaintType	Branch1	Branch2	Branch3
complaintType1	0	0	2
complaintType2	0	0	0
complaintType3	0	0	1
complaintType4	0	0	0
complaintType5	0	0	0

Sorry if the formatting is a bit rubbish but that gives you an idea of what i have so far.

When looping through the recordset using a while loop and the property .EOF and the method .MoveNext() i can't get it to also display the column names.

The sql column names are dynamically written so i cant just write the columns into the table myself.

Is there a way of also showing the column names of a recordset?
 
try this:

Code:
	Response.Write "<table border=1>"
    If Not rsObj.EOF Then
        Response.Write "<tr>"
        For x = 0 To rsObj.Fields.Count - 1
            Response.Write "<th>" & rsObj(x).Name & "</th>"
            Response.Flush()
        Next
        Response.Write "</tr>"
        
        Do While Not rsObj.EOF
            Response.Write "<tr>"
            For x = 0 To rsObj.Fields.Count - 1
                Response.Write "<td>" & rsObj(x)& "&nbsp;</td>"
            Next
            Response.Write "</tr>"
            rsObj.MoveNext
        Loop
    End If
    Response.Write "</table>"

-DNG
 
Thanks for the replies.

Here is my result in asp javascript:
Code:
    <%
	
	Response.Write("<table border='1px' cellpadding='0'>");
   	if(!myRS.EOF)
	{
        Response.Write("<tr>");
        //loop through each field
		for(x=0; x<=myRS.Fields.Count - 1; x++)
		{
            Response.Write("<th>"); 
			//write the name of the field
			Response.Write(myRS(x).Name);
			Response.Write("</th>");
       	}
        Response.Write("</tr>");
        
		//loop through each record in the recordset
        while(!myRS.EOF)
		{
            Response.Write("<tr>");
            //loop through each field in the record
			for(x=0; x<=myRS.Fields.Count - 1; x++)
			{
                Response.Write("<td>");
				//write the data for the record's field
				Response.Write(myRS(x));
				Response.Write("</td>");
            }
            Response.Write("</tr>");
            //move to next record in recordset
			myRS.MoveNext();
        }
    }
    Response.Write("</table>");
	%>

I made a few changes.

I removed
Code:
Response.Flush();
Mainly because i don't think it does anything unless i do Response.Buffer() = True; and i am also unsure to why i would need to do .Flush();

I also changed:
Code:
Response.Write "<th>" & rsObj(x).Name & "</th>"
To
Code:
Response.Write("<th>"); 
Response.Write(myRS(x).Name);
Response.Write("</th>");
Purely because it wasn't working, it just kept writing out a lot of zeros.

Thanks for the help and any info on the changes i made would be good.
 
Buffering is on by default. If you had a page that took a long time to generate, a Response.Flush() after each chunk (or even row) will keep the user happier. Another reason to use it is if you have a page that's erroring out, you can get what it's generated so far by flushing it and have the error tacked onto the end instead of just getting an error page alone.

But in this case I see no need for a Flush().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top