Groups > Asp .Net > Advanced ASP.NET Architecture > Re: Component Class for DAL




Component Class for DAL

Component Class for DAL
Thu, 27 Mar 2008 15:27:23 +000
Hi all,

I have done much reading about creating n-tier applications and would like to
have the speed of using a tableadapter to generate the code and the flexibility
of using business objects.

My question is, can I use the component class to create my business objects
while being able to use the designer tools in VS 2005 to create the
dataadapters, commands and query?

I am not an expert so would like to know your perspective?

Thanks for your time.
Post Reply
Re: Component Class for DAL
Sat, 29 Mar 2008 00:52:06 +000
Try this.

Mark up:

It works at framework 1.1,
however in 2.0, you can use the much simple way by placing sqlcommands in data
adapter and putting everything in dataset, then use server controls (repeater,
datagrid..etc) to display them
I just show you what can be done at 1.1 if the compiler does not recognise the
easiest way instead wants you to use business object.

forumstdpage.vb

Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.Security
Imports System.Configuration

Namespace FORUM

   Public Class PageBase
    Inherits Page
   
      Public Function getConnStr() As String
      
 Dim dsn As String =
ConfigurationSettings.AppSettings("connectionString")
 
 Return dsn
      
      End Function
      
   End Class

End Namespace

 discussiondb.vb

Imports System
Imports System.Data
Imports System.Data.OleDb


Namespace FORUM

   Public Class DiscussionDB 
   
      Dim m_ConnectionString As String
      
      Public Sub New(dsn As String)
         m_ConnectionString = dsn
      End Sub

      Public Function GetDiscussDataSet() As DataSet

          Dim OleDbConnection As New OleDbConnection(m_ConnectionString)
          Dim OleDbAdapter1 As New OleDbDataAdapter("SELECT * FROM
discussion", OleDbConnection)

          Dim discussion As New DataSet()
          OleDbAdapter1.Fill(discussion, "discussion")

          Return Discussion
          
      End Function

 Web.Config file (syntax slightly different from .NET framework version) 

<configuration>
 <appSettings>
  <add key="connectionString"
value="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\wwwroot\testvw\store.mdb" />
  </appSettings>

 

In SDK Cmd Prompt, goto the directory of the business objects you store (eg. c:
\ inetpub\ wwwroot \ forum \ components ), and compile them with this:

vbc /t:library /out:..\..\bin\Forum.dll /r:System.dll /r:System.Data.dll
/r:System.XML.dll discussiondb.vb forumstdpage.vb

the Forum.dll will store in the bin folder for reference (in this case, the bin
folder is at c: \ inetpub \ wwwroot \ bin )

 

discussion.aspx

<%@ Page Language="VB" Inherits="FORUM.PageBase"
src="components/forumstdpage.vb" %>

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

<script language="VB" runat="server">

<html>

  Sub Page_Load(sender As Object, e As EventArgs)
  
   If Not IsPostBack Then
   
    Dim inventory As New FORUM.discussionDB(getConnStr())
    MyList.DataSource = inventory.GetDiscussDataSet()
    MyList.DataBind()

   End If
  
  End Sub
  
</script>

<body>
<form runat="server">
<table width="100%" border="0" cellspacing="0"
cellpadding="0">
  <tr>
    <td><ASP:DataList id="MyList"
DataKeyField="discussionid" runat="server"> 
                <ItemTemplate>
                      <table cellpadding=10 style="font: 10pt
verdana">
                        <tr> 
                          <td valign="top"> <b>No. :
</b> <%# DataBinder.Eval(Container.DataItem, "discussionid")
%> <br> <b>Title: </b><br> <asp:linkbutton
Text='<%# DataBinder.Eval(Container.DataItem, "dTitle") %>'
CommandName="Select" style="color:darkred"
runat="server"/> <br> </td>
                        </tr>
                      </table>
                      </ItemTemplate>
              </ASP:DataList> 

</form></body></html>
Post Reply
about | contact