1

Please consider the following scenario.

You have a stored proc returning multilple result sets for queries run for reporting purposes.

The proc also use table valued parameters (SQL 2008). http://www.sommarskog.se/arrays-in-sql-2008.html#LINQ_EF

These result sets are read-only, meaning you don't have to worry about updates, deletes, or inserts.

These result sets do not map to any table in the Database; these are reporting results, not mirrors of database tables.

Now, my background is asp classic, and while I have been trying to read up on all the .NET data access strategies, it seems: (1) MS is pushing Entity Framework (2) MS has a plethora of data access/architecture strategies (3) the entire topic is a continual subject of debate (4) the circumstances of the project help dictate the proper data access strategy, and ORM does not seem optimized for this data access scenario (5) there is no out-of-the-box solution for mapping multiple result set stored procs

Also, I'm looking for full control over the html output, so if using code behind, only Repeaters and Literals would be used for databinding.

I find the Web Forms programming model fails to deliver complete separation of code and content, and can be just as "spaghetti" as asp classic. (The MVC approach seems much more like asp classic, but this project is already WebForms.)

I think this would be fairly easy to implement by having data access and inline scripting <% %> on the same page. Here's an example of the kind of code that would result:

<%
' data access (skipping a bunch of code)....
Dim myDataSet As New DataSet()
myCommand.Fill(myDataSet)
'...etc.    

Teachers = myDataSet.Tables(0).Rows
Students = myDataSet.Tables(1).Rows
'... etc.%>

then

<%  If Teachers.Count > 0 Then%>
<table><%  For Each _Teachers In Teachers%>
    <tr>
        <td><%= _Teachers(0)%></td>
        <td><%= _Teachers(1)%></td>
    </tr><% Next %>    
</table>
<%  Else%>Hey, there's no records.<% End If %> 

(When I try to separate the data access in code behind under the "Protected Sub Page_Load...", the variables in the code behind that I use in the .aspx page keep giving the error: "[variable] is not declared. It may be inaccessible due to its protection level.")

If the result sets from the proc are not strongly typed, is it the end of the world, awful programming practice, a maintainability nightmare, the worst possible choice, etc.?

If the result sets from the stored proc ought to be strongly typed, then what is the most effective way of going about this? (I'm not finding straightforward tutorials on this topic.)

Thanks in advance for any help.

mg1075
  • 17,985
  • 8
  • 59
  • 100

1 Answers1

0

It sounds like you're asking whether you should continue to use ADO.NET containers or custom domain classes for your data.

If you feel you need the strong typing of a class, and some validation of input, or other logic applied to your data before display, then consider writing a method to convert a DataTable into a collection of YourClass. Otherwise, if this is for display, then there's nothing wrong with sticking with the DataTable. If you're providing an interface for other components, or need the features that a class would give you, go for the strong typing of a class.

public IEnumerable<Teacher> ConvertToTeachers(DataTable dt)
{
    foreach (var row in dt.Rows.AsEnumerable())
    {
        //create a teacher from this row. modify row indexers as required.
        yield return new Teacher{ TeacherName = row["Name"].Value,
                                  Location = row["Location"].Value };
    }   
}

On the presentation tier, consider leveraging the server controls that ASP.NET webforms provides you. They provide data-binding capabilities that remove all that looping.

  • Ensure your gridview is designed as you like. Proper columns containing the data you want: regular bound fields and hyperlinks.

  • bind your data to the grid.

 gridViewTeachers.DataSource = myDataSet.Tables(0).Rows
 gridViewTeachers.DataBind
p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • Thanks for replying. RE: "...consider writing a method to convert a DataTable into a collection of YourClass". I guess I'm too much of an OOP newbie to do this part on my own. Do you think you could offer any further suggestions or resource pointers? – mg1075 Dec 11 '10 at 04:11
  • 1
    @mg1075 - Check out the code I created to help illustrate the solution. http://pastebin.ca/2017916 – p.campbell Dec 13 '10 at 02:47
  • I'm working in VB, not C#, and apparently VB's support for "yield" is lacking? http://stackoverflow.com/questions/97381/yield-in-vb-net – mg1075 Dec 13 '10 at 20:55
  • In that case, you can create the new object, add to a local `List`, and simply return that `List` instead of `IEnumerable` – p.campbell Dec 14 '10 at 17:24
  • Alas, my skills are lacking in that area. I've formalized the question over at: http://stackoverflow.com/questions/4419599/how-to-map-ado-net-datatables-from-multiple-result-set-stored-procedure-to-custom – mg1075 Dec 17 '10 at 00:43