Library tutorials & articles

Replicating GetRows in .NET

Introduction

I believe .NET is the most exciting new development platform to date, with cumulative features surpassing many others as a whole. Migrating your legacy ASP code can be a daunting task given that you're learning as you go, but if you had some perusal of the framework it'll be a little less cumbersome. Of course with .NET, the ability to co-exist with legacy ASP code is fine, and you can even port in legacy code through the help of the exposing the legacy ADO components. But anyone serious about .NET would benefit themselves in taking full advantage of .NET and all it has to offer.

As you begin to delve deeper into the means of migrating your old code to .NET, you may run into a few stumbling blocks, and more so when you embark towards C# in particular, because of syntax differences and some VBScript/ASP coding methods you may have been accustomed to. In any event, one feature I have found to be missing in sorts in migration is the simple ADO method of GetRows which copies your data results into an two-dimensional variant array in the form of recordset.GetRows(Rows, Start, Fields) .

In .NET however this exact method is not to be found, or is it? Well it is…kind of. Now, most people learning .NET upon first view might see a general template driven approach in which applications can be developed. They learn about DataGrids, Web controls, code-behind, data readers, and so on, so this all may give the immediate impression of developmental building blocks without much precise control of one's code. Well nothing could be further from the truth. Replicating GetRows in .NET is easier than you think and it gives you precise control in the detail of your data and its display. Having said that, one feature not available with this method is the implemention of the NextRecordSet feature, whereby multiple batch SQL statements can be consecutively run. Nevertheless, the .NET DataReader duplicates this functionality with Reader.NextResult() , but not within the context of this example.

This article at the very least will inform you about the DataTable object and how to use it.

To now get back on track, let's explore the classic ASP method of displaying a table of data with GetRows and then replicate it exactly in .NET.

Comments

  1. 30 Apr 2005 at 23:36

    <%@ Page Language="C#" Debug="False" Strict="True" Explicit="True" Buffer="True"%>


    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>


    <script language="C#" runat="server">


    StringBuilder grTable;
    string sqlStr;
    int TotalRows, TotalFlds, c, r;


       void Page_Load(Object Source, EventArgs E) {


         sqlStr = "Select lname As LastName, fname As FirstName, empid As ID, hiredate As [Hired On] from Employee";
         SqlConnection objConnect = new SqlConnection ("server=(local);uid=sa;pwd=;database=Pubs;");
         SqlDataAdapter objDataAdapter = new SqlDataAdapter (sqlStr.ToString(), objConnect);
         DataSet objDS = new DataSet();


         //Create and Fill Info Datatable with results
         objDataAdapter.Fill (objDS,"Info");


         //Close and clear our connections
         objConnect.Close();
         objConnect = null;


         //Declare name variable as a DataTable
         DataTable GetRows = objDS.Tables ["Info"];


         //Get Table Info
         TotalRows = GetRows.Rows.Count;
         TotalFlds = GetRows.Columns.Count;
         grTable = new StringBuilder();
         grTable.Append ("<TABLE border=1 Width=60%>");
         grTable.Append ("<TR>");


         //Loop through data
         //Loop through the Columns Fields
         for (c = 0; c <= TotalFlds-1; c++) {
           grTable.Append ("<TD><B>" + GetRows.Columns[c].ToString() + "</B></TD>");
         }
         grTable.Append ("</TR>");


         //First header row is now closed and we loop through our database rows
         for (r = 0; r <= TotalRows-1; r++) {
           grTable.Append ("<TR>");
           grTable.Append ("<TD>" + GetRows.Rows[r][0].ToString() + "</TD>");
           grTable.Append ("<TD>" + GetRows.Rows[r][1].ToString() + "</TD>");
           grTable.Append ("<TD>" + GetRows.Rows[r][2].ToString() + "</TD>");
           grTable.Append ("<TD>" + GetRows.Rows[r][3].ToString() + "</TD>");
           grTable.Append ("</TR>");
         }
         grTable.Append ("</TABLE>");
         objDataAdapter = null;
         objDS = null;
       }
    </script>


    <html>
    <body>
    <%
     Response.Write (grTable.ToString());
     grTable = null;
    %>
    </body>
    </html>


    -Jimmy Markatos

  2. 29 Apr 2005 at 15:47

    Do you have an example of this same concept using C#?

  3. 03 Mar 2005 at 02:42

    No, it's not pathetic and Yes, it is a technique that clearly your lack of insight allowed you to totally miss. Sure GetRows is what it is. Nevertheless, one project app fully benefited from this technique in a way any other technique would simply not do.


    This has nothing to do with portraying .NET's magnificent features or coding the “.NET way” All this shows is a unique methodology in "Replicating GetRows in .NET" in light of a particular need, when migrating a specific functionality from old ASP, and replicating as is to .NET, period!


    So if you don't agree or YOU missed the point, then it's an oversight on your part. Not considering, Developer Fusion deemed it worthy to be added to this site.

  4. 18 Jan 2005 at 09:12

    It sure can - I've just corrected the article to reflect this

  5. 18 Jan 2005 at 09:10

    The author has indeed closed the connection at the end of the code (I've moved this further up now to make this clearer) - but the actual recordset is automatically closed the moment the call to the adapters "Fill" method is called - the recursive part of the code is on the "disconnected" DataSet.

  6. 18 Jan 2005 at 07:36

    The whole point of GetRows is to open a conn, get the data and close the conn.
    not leave it open while you loop all data as you would exactly like a recordset.


    Pathetic.

  7. 04 May 2004 at 11:28

    Forget about oops buddy, when ur migrating tons of code on a deadline.


    But do we need to close the connection in the end, cant the datatable sustain data when the collection is closed.


  8. 27 Apr 2004 at 23:29

    Hi!


    I'm trying to divide data from a table in different excel worksheets files.


    Nombre    Dato           Nombre    Dato       Nombre    Dato
    a    1                   a           1               b              45
    a    10                 a           10             b              38
    b    45
    b    38
           

  9. 14 Apr 2004 at 05:10
    please dont do what this article says....its the worst way to code in .NET
  10. 12 Apr 2004 at 10:26
    I really don't think this article is a good idea at all. You are really showing people how NOT to use .NET to its fullest.  Why you would not drag a table on to your form and bind your dataset to it in 2-3 lines of code is beyond me.  Just because you CAN use this horribly archaic method of building HTML strings in code doesn't mean you should.  In fact, it should be avoided at all costs.  It's nearly akin to embedding SQL in your code rather than using stored procedures.

    As far as "control over display" you can really accomplish almost anything by using the format properties for each column and/or formatting the data in the SQL or Stored Proc.  If you needed something really crazy, you could even add some code to the ItemDataBound event.  That's a useful discussion in and of itself since you can also add formatting based on data elements - such as highlighting a row where a product is on sale or something like that.
  11. 01 Jan 1999 at 00:00

    This thread is for discussions of Replicating GetRows in .NET.

Leave a comment

Sign in or Join us (it's free).

Dimitrios Markatos Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and deskto...
AddThis

Related podcasts

Events coming up

  • Jul 7

    DTC 70-528 Session 7: Chapter 12

    Greenwood Village, United States

    Due to lack of interest of the 5th Monday meetup, we will continue as originally scheduled. The topic of the night will be "Chapter 12 - Creating ASP.NET Mobile Web Applications", taught by RJ Hatch. It is a fairly small chapter, so we can discuss other topics as well. Pizza and beverages will be provided on a donation basis.

We'd love to hear what you think! Submit ideas or give us feedback