Question SqlDataSource question

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
So I'm refactoring code for an asp.net web app. I'm new to asp.net, and was surprised to learn about the data-binding within controls. It seems nice and simple, however it also seems to be mingling data code with logic (within this implementation). My question is how to use data-binding while still keeping the queries separated from the logic. If there isn't a way to do that, what would the best practice be in accomplishing the same task?

My concrete example is the following

VB.NET:
'myPage.aspx.designer.vb
    Protected WithEvents SqlHistoryTable As Global.System.Web.UI.WebControls.SqlDataSource

'myPage.aspx
<asp:SqlDataSource ID="SqlHistoryTable" runat="server" ProviderName="System.Data.OleDb">
            </asp:SqlDataSource>
'...
<dx:ASPxGridView ID="dgrHistory" ClientInstanceName="dgrHistory" runat="server" DataSourceID="SqlHistoryTable"
                    Width="831px" EnableCallBacks="False" KeyFieldName="Date">
'...

'myPage.aspx.vb --->This is where my conflict with data-binding occurs<---
SqlHistoryTable.SelectCommand = QueryFactory.GetHistoryTableCommand(
            Convert.ToDateTime(dtpStartDate.Text), Convert.ToDateTime(dtpEndDate.Text))

Originally I wanted my design to have a seperate database class to be closest to the data. A QueryFactory to store all my queries which only my DataAccess class would call. When I ran into the data-binding however, I am now calling my QueryFactory outside my DataAccess class which seems odd to me since it's not what I had intended.

thanks for your help,
Phil
 
Let me preface this post by saying that it is not an answer to your question, but rather some advice that may help you in the future.

I've done very little Web Forms development myself, in large part because I never liked it. I've been doing quite a lot of web development lately using ASP.NET MVC, which is on the verge of releasing version 3 (currently at RC2). It may not be an option for this current project but, if you're genuinely interested in separation of concerns in ASP.NET, you should switch from Web Forms to MVC for future projects. Even though I'm historically a WinForms developer, MVC feels much more natural to me for web development, even though Web Forms tries hard to imitate WinForms and MVC is nothing like it. MVC lacks the server controls that Web Forms provides but, with the addition of a data grid in version 3 and the availability of jQuery plugins, that's not really an issue.
 
Thanks for your reply - and I'm personally not a fan of web forms at all. Mainly any type of wysiwyg development annoys me (unless it's very small scale). Each page's design view is so cluttered and don't look anything like what you would see in a browser. It's what I have to work with though, so mainly i'm just refactoring all the vb files and structure. That is interesting about asp.net mvc; I'll definitely have to take a look. If I finally get to create something here at this new job (as opposed to debug/fix/refactor), I'd like to learn a framework built over a nicer foundation than web forms.
 
Last edited:
Back
Top