Search code examples
asp.netuser-controlsascx

Separating UI from Data Access in ascx user control


What is the "best practice" for designing ascx user controls regarding separating the UI from the Data Access? Should my user control use 3 tier archetecture as in my projects or can I do the data acess from within the user control?


Solution

  • You should never access the database from a user control. You should create a class for accessing the database, and everything within your application should use that class. Basically, the methods in the class would be wrappers around your stored procedure calls, but all that the application (and therefore user controls) sees is a method with the needed parameters. No knowledge of the database from the application's point of view. That allows you to make changes to the database without changing your application.

    This link might help you:

    http://www.simple-talk.com/dotnet/.net-framework/.net-application-architecture-the-data-access-layer/