In ASP, I used to create a recordset and loop through it and display the info as such:
<% set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset") rs.Open "SELECT * FROM Customers", conn
do until rs.EOF for each x in rs.Fields Response.Write(x.name) Response.Write(" = ") Response.Write(x.value & "<br />") next Response.Write("<br />") rs.MoveNext loop
rs.close conn.close %>
Can this still be done in ASP.Net or is there a better way to do this?
Unfortunately, you have to use control objects in asp.net. Classic ASP was more forgiving and didn't have as stringent requirements as asp.net does. Can you have in-line vbscript code in your page, you could but remember the old addage, just because we can
doesn't mean we should. :)
-------------- EDIT -------------------
I stand corrected, I forgot about response write but you should be writing your output to controls as a best practice. :)
Metal is indeed correct. You can do response.write instead of console.writeline. But you have to remember that classic ASP allowed developers to bend the rules which left them more succeptable to attack, something asp.net makes it harder to do.
Mark as answer posts that helped you.
Marked as answer by bbcompent1 on Feb 21, 2012 03:46 PM
JumpMaster
Member
30 Points
21 Posts
Recordsets
Feb 14, 2012 12:25 PM|LINK
Hello All,
In ASP, I used to create a recordset and loop through it and display the info as such:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
set rs = Server.CreateObject("ADODB.recordset")
rs.Open "SELECT * FROM Customers", conn
do until rs.EOF
for each x in rs.Fields
Response.Write(x.name)
Response.Write(" = ")
Response.Write(x.value & "<br />")
next
Response.Write("<br />")
rs.MoveNext
loop
rs.close
conn.close
%>
Can this still be done in ASP.Net or is there a better way to do this?
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: Recordsets
Feb 14, 2012 12:56 PM|LINK
In ASP.NET, you are always better off to use what is called a Data Reader control. Hang on, I'll be back with an asp.net c# equivalent.
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: Recordsets
Feb 14, 2012 01:00 PM|LINK
You can do this in this fashion:
string connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\myDatabase.mdb;User Id=admin;Password=;"; static void HasRows(SqlConnection connection) { using (connection) { SqlCommand command = new SqlCommand( "SELECT CategoryID, CategoryName FROM Categories;", connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1)); } } else { Console.WriteLine("No rows found."); } reader.Close(); } }JumpMaster
Member
30 Points
21 Posts
Re: Recordsets
Feb 14, 2012 01:16 PM|LINK
Thank you for your swift reply
,
I will try your solution and work with it.
Cheers
JumpMaster
Member
30 Points
21 Posts
Re: Recordsets
Feb 15, 2012 08:58 AM|LINK
Out of curiosity, is there a way to do this with out control objects?
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: Recordsets
Feb 15, 2012 12:06 PM|LINK
Unfortunately, you have to use control objects in asp.net. Classic ASP was more forgiving and didn't have as stringent requirements as asp.net does. Can you have in-line vbscript code in your page, you could but remember the old addage, just because we can doesn't mean we should. :)
-------------- EDIT -------------------
I stand corrected, I forgot about response write but you should be writing your output to controls as a best practice. :)
MetalAsp.Net
All-Star
112157 Points
18249 Posts
Moderator
Re: Recordsets
Feb 15, 2012 12:19 PM|LINK
Indeed you can. You can use Response.Write() in ASP.NET as well. I said you can not you should.
bbcompent1
All-Star
32994 Points
8509 Posts
Moderator
Re: Recordsets
Feb 15, 2012 12:24 PM|LINK
Metal is indeed correct. You can do response.write instead of console.writeline. But you have to remember that classic ASP allowed developers to bend the rules which left them more succeptable to attack, something asp.net makes it harder to do.