Professional Documents
Culture Documents
Microsoft Word - DAL+
Microsoft Word - DAL+
Các bạn chỉ cần dựa theo cấu trúc để xây dựng.
Right-click on the "ThingsForSale" folder you just created and select "Add New Item"
In the "Add New Item" box that opens, click on "Class" under "Visual Studio Installed
Templates", enter "ThingsForSaleInfo.vb" in the "Name" box and click the "Add" button.
Namespace YourCompany.Modules.ThingsForSale
End Class
End Namespace
"controller class"
Right-click on the "ThingsForSale" folder and select "Add New Item".
In the "Add New Item" box that opens, click on "Class" under "Visual Studio Installed
Templates", enter "ThingsForSaleController.vb" in the "Name" box and click the "Add"
button.
The class will now open up in the designer.
Imports System
Imports System.Collections.Generic
Imports System.Data
Namespace YourCompany.Modules.ThingsForSale
<DataObjectMethod(DataObjectMethodType.Insert)> _
Public Shared Sub ThingsForSale_Insert(ByVal ThingsForSaleInfo As ThingsForSaleInfo)
DataProvider.Instance().ExecuteNonQuery("ThingsForSale_Insert", ThingsForSaleInfo.ModuleId,
GetNull(ThingsForSaleInfo.UserID), GetNull(ThingsForSaleInfo.Category.ToString),
GetNull(ThingsForSaleInfo.Description.ToString), GetNull(ThingsForSaleInfo.Price))
End Sub
<DataObjectMethod(DataObjectMethodType.Delete)> _
Public Shared Sub ThingsForSale_Delete(ByVal ThingsForSaleInfo As ThingsForSaleInfo)
DataProvider.Instance().ExecuteNonQuery("ThingsForSale_Delete", ThingsForSaleInfo.ID)
End Sub
<DataObjectMethod(DataObjectMethodType.Update)> _
Public Shared Sub ThingsForSale_Update(ByVal ThingsForSaleInfo As ThingsForSaleInfo)
DataProvider.Instance().ExecuteNonQuery("ThingsForSale_Update", ThingsForSaleInfo.ID,
ThingsForSaleInfo.ModuleId, GetNull (ThingsForSaleInfo.UserID),
GetNull(ThingsForSaleInfo.Category.ToString), GetNull(ThingsForSaleInfo.Description.ToString),
GetNull(ThingsForSaleInfo.Price))
End Sub
<DataObjectMethod(DataObjectMethodType.Select)> _
Public Shared Function ThingsForSale_SelectAll(ByVal ModuleId As Integer) As List(Of ThingsForSaleInfo)
Return CBO.FillCollection(Of
ThingsForSaleInfo)(CType(DataProvider.Instance().ExecuteReader("ThingsForSale_SelectAll", ModuleId),
IDataReader))
End Function
End Class
End Namespace
This time we did a lot. However, we did a lot without using a lot of code.
We have constructed a class called "ThingsForSaleController" that has 4 public methods. Each
of these methods uses one of the new methods of the DAL+ to execute stored procedures.
We haven't created the stored procedures yet. We will do that in a later step. For now, we have
simply created a method for each task we will need to perform with the database:
· Delete - (ThingsForSale_Delete)
· Insert - (ThingsForSale_Insert)
· Select - (ThingsForSale_SelectAll)
· Update - (ThingsForSale_Update)
Note: A protected method, "GetNull" is used to pass the proper null value to the database for any
values that could be empty.
The Delete, Insert, and Update methods accept the "ThingsForSaleInfo" class that was created
previously as a parameter.
The DAL+ is comprised of 4 methods used to execute stored procedures. The methods are:
· ExecuteNonQuery - Used to execute a stored procedure that will not return a value.
· ExecuteReader - Used to execute a stored procedure that will return multiple records.
· ExecuteScalar - Used to execute a stored procedure that will return a single value.
· ExecuteSQL - Used to execute a sql statement.
The Delete, Insert, and Update methods use the ExecuteNonQuery method of the DAL+ and
the SelectAll method uses the ExecuteReader method of the DAL+ (the ExecuteScalar and
ExecuteSQL methods of the DAL+ are not used in this tutorial).
Below is an explanation of the format used to implement the DAL+. The ExecuteReader
method that is used in the "ThingsForSale_SelectAll" method is used in this example:
"View" control
· The Web User Controls and their associated code behind files reside in the
"DesktopModules" directory.
· All other code resides in the "App_Code" directory.
We have created the DAL+ code in the "App_Code" directory. We will now complete the
module by creating the Web User Control in the "DesktopModules" directory.
Create The Directory
Right-click on the "DesktopModules" folder and select "New Folder"
The "ViewThingsForSale.ascx" file will be created in the "ThingsForSale" folder under the
"DesktopModules" folder.
Clicking the "plus" icon next to the file will display the associated code behind file
"ViewThingsForSale.ascx.vb".
Create The Grid View and Form View
Double-click on the "ViewThingsForSale.ascx" file and it will open up in the main editing
window. Right now the page will be blank. Click the "Source" button at the bottom of the page
to switch to source view.
Replace ALL the code with the following code: <%@ Control language="vb"
Inherits="YourCompany.Modules.ThingsForSale.ViewThingsForSale" CodeFile="ViewThingsForSale.ascx.vb"
AutoEventWireup="false" Explicit="True" %>
<%@ Register Assembly="DotNetNuke.WebUtility" Namespace="DotNetNuke.UI.Utilities" TagPrefix="cc1" %>
<%@ Register TagPrefix="dnn" TagName="Audit" Src="~/controls/ModuleAuditControl.ascx" %> <br />
Click the "Design" button at the bottom of the design window and the screen will look like the
image on the right.
We placed 4 controls on the page:
· ObjectDataSource Control
o This is at the top of the image on the right. It is labeled "ObjectDataSource".
This control is bound to the "ThingsForSaleController" class created in the
earlier step.
· GridView Control
o This control is immediately under the "ObjectDataSource" control. This control
is bound to the "ObjectDataSource" control and is configured to Select, Update,
and Delete.
· LinkButton Control
o This is a simple LinkButton that will display messages and when clickable, will
allow the FormView control below it to display.
· FormView Control
o This control will allow a user to enter an item into the database. it is also bound to
the "ObjectDataSource" control and is configured to Insert.
Click on it and the configuration menu will appear. click on "Configure Data Source".
Here we indicate "ModuleId" with a default value of "00". This is just a "place holder".
This is how we indicate that this class will be used to pass data between the
"ObjectDataSource" control and the "ThingsForSaleController" class.
Create the code behind
Double-click on the "ViewThingsForSale.ascx.vb" file so that it opens up in the design
window.
Replace ALL the code with the following code: Imports DotNetNuke
Imports System.Web.UI
Imports System.Collections.Generic
Imports System.Reflection
Imports DotNetNuke.Security.PortalSecurity
Namespace YourCompany.Modules.ThingsForSale
Inherits Entities.Modules.PortalModuleBase
End Sub
e.InputParameters("ModuleId") = ModuleId.ToString
End Sub
e.Values.Item("UserID") = Entities.Users.UserController.GetCurrentUserInfo.UserID
e.Values.Item("ModuleId") = ModuleId.ToString()
e.Values.Item("ID") = 0
End Sub
Me.FormView1.Visible = False
End Sub
Me.FormView1.Visible = True
End Sub
Me.FormView1.Visible = False
Add_My_Listing_LinkButton.Text = "Update Successful - Add Another Listing"
End Sub
End Sub
End Class
End Namespace
Hopefully this will help those using Object Data Sources with DotNetNuke. There is a problem
of grabbing the current “ModuleId”.
This is a very important value that is exposed by the DotNetNuke core code. This value tells you
which instance of the module you are working with. This is important because a person can place
multiple instances of your module on a single page. You can’t have them click a button on one
instance of the module and return data from another instance of the module.
This event fires when the “Select” event is called in the "ObjectDataSource" control, but before
the control passes the value to the “SelectAll” method in the "ThingsForSaleController" class.
This allows you to pass the “ModuleId” to the "ThingsForSaleController" class (and on to the
"ThingsForSale_SelectAll" stored procedure so it returns the proper data).
In the SOLUTION EXPLORER right-click on the "Default.aspx" page and select SET AS
START PAGE
From the toolbar select DEBUG then START WITHOUT DEBUGGING
GO
/** Create Stored Procedures **/
We ran a SQL script that created the table and the stored procedures.
The script commands "{databaseOwner}" and "{objectQualifier}" indicate that they are to be
replaced by configuration settings in the web.config file. Normally "{databaseOwner}" is set to
".dbo" and "{objectQualifier}" is set to nothing (it would not have a setting). However, if
alternate settings were indicated in the web.config file, those settings would be inserted into the
script.
You must have the "Run as Script" box checked for this replacement to happen.