Microsoft Word - DAL+
Transcript of Microsoft Word - DAL+
-
8/9/2019 Microsoft Word - DAL+
1/26
Ti li
Cc b n ch c n d a theo c xy d ng.
Setting Up The Module
We will create the module using the following steps:
Create the DAL+o Create the "info class"o Create the "controller class"
Create the "View" controlo Create The Grid View and Form Viewo Create the code behind
Create the tables and stored procedures
Create The Directories
Open your DotNetNuke website in Visual Studio.
Create the DAL+
First we will create the Data Access Layer using the DAL+ method. This is the class that
communicates with the database.
Right-click on the "App_Code" folder and select "New Folder"
-
8/9/2019 Microsoft Word - DAL+
2/26
Name the new folder "ThingsForSale"
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.
The class will now open up in the designer.
-
8/9/2019 Microsoft Word - DAL+
3/26
Replace ALL the code with the following code:
Namespace YourCompany.Modules.ThingsForSale
PublicClass ThingsForSaleInfo
Private _ModuleId AsIntegerPrivate _ID AsIntegerPrivate _UserID AsIntegerPrivate _Category AsString
-
8/9/2019 Microsoft Word - DAL+
4/26
Private _Description AsStringPrivate _Price AsDouble' initialization
PublicSubNew()MyBase.New()EndSub' ' Gets and sets the Module Id' PublicProperty ModuleId() AsIntegerGetReturn _ModuleIdEndGetSet(ByVal value AsInteger)_ModuleId = valueEndSetEndProperty' ' Gets and sets the Item ID' PublicProperty ID() AsIntegerGetReturn _IDEndGetSet(ByVal value AsInteger)_ID = valueEndSetEndProperty' ' Gets and sets the UserID
' PublicProperty UserID() AsIntegerGetReturn _UserIDEndGetSet(ByVal value AsInteger)_UserID = valueEndSetEndProperty' ' Gets and sets the Category' PublicProperty Category() AsString
GetReturn _CategoryEndGetSet(ByVal value AsString)_Category = valueEndSetEndProperty' ' Gets and sets the Description' PublicProperty Description() AsStringGetReturn _Description
-
8/9/2019 Microsoft Word - DAL+
5/26
EndGetSet(ByVal value AsString)_Description = value
EndSetEndProperty' ' Gets and sets the Price' PublicProperty Price() AsDoubleGetReturn _PriceEndGetSet(ByVal value AsDouble)_Price = valueEndSetEndProperty
EndClass
EndNamespace
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 InstalledTemplates", enter "ThingsForSaleController.vb" in the "Name" box and click the "Add"button.
-
8/9/2019 Microsoft Word - DAL+
6/26
The class will now open up in the designer.
Replace ALL the code with the following code:
Imports SystemImports System.Collections.GenericImports System.Data
-
8/9/2019 Microsoft Word - DAL+
7/26
Namespace YourCompany.Modules.ThingsForSale
PublicClass ThingsForSaleController
_PublicSharedSub 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))EndSub
_PublicSharedSub ThingsForSale_Delete(ByVal ThingsForSaleInfo As ThingsForSaleInfo)
DataProvider.Instance().ExecuteNonQuery("ThingsForSale_Delete", ThingsForSaleInfo.ID)EndSub
_PublicSharedSub 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))EndSub
_PublicSharedFunction ThingsForSale_SelectAll(ByVal ModuleId AsInteger) As List(OfThingsForSaleInfo)
Return CBO.FillCollection(OfThingsForSaleInfo)(CType(DataProvider.Instance().ExecuteReader("ThingsForSale_SelectAll", ModuleId),IDataReader))
EndFunction
PrivateSharedFunction GetNull(ByVal Field AsObject) AsObjectReturn Null.GetNull(Field, DBNull.Value)
EndFunction
EndClass
EndNamespace
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. Eachof these methods uses one of the new methods of the DAL+ to execute stored procedures.
-
8/9/2019 Microsoft Word - DAL+
8/26
We haven't created the stored procedures yet. We will do that in a later step. For now, we havesimply 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 anyvalues that could be empty.
The Delete, Insert, and Update methods accept the "ThingsForSaleInfo" class that was createdpreviously as a parameter.
The SelectAll methods takes an integer as a parameter and RETURNS a "ThingsForSaleInfo"
class.
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.
-
8/9/2019 Microsoft Word - DAL+
9/26
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
ExecuteSQLmethods 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:
We have completed creating the DAL+ for our module.
Complete The Code
The steps that remain are:
Create the "View" controlo Create The Grid View and Form Viewo Create the code behind
Create the tables and stored proceduresRegister the module in DotNetNuke
Remember, A DotNetNuke module resides in 2 directories:
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 themodule by creating the Web User Control in the "DesktopModules" directory.
-
8/9/2019 Microsoft Word - DAL+
10/26
Create The Directory
Right-click on the "DesktopModules" folder and select "New Folder"
Name the new folder "ThingsForSale"
Create the "View" control
Right-click on the "ThingsForSale" folder you just created and select "Add New Item"
-
8/9/2019 Microsoft Word - DAL+
11/26
In the "Add New Item" box that opens:
Click on "Web User Control" under "Visual Studio Installed Templates".Enter "ViewThingsForSale.ascx" in the "Name" box.Make sure the "Place code in a separate file" box is checked.Click the "Add" button.
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".
-
8/9/2019 Microsoft Word - DAL+
12/26
Create The Grid View and Form View
Double-click on the "ViewThingsForSale.ascx" file and it will open up in the main editingwindow. Right now the page will be blank. Click the "Source" button at the bottom of the pageto switch to source view.
Replace ALL the code with the following code:
There are no Things 4 Sale
-
8/9/2019 Microsoft Word - DAL+
13/26
Add My
Listing
Category:HomeOfficeElectronicsMisc.Price: $Description:
Click the "Design" button at the bottom of the design window and the screen will look like the
image on the right.
-
8/9/2019 Microsoft Word - DAL+
14/26
We placed 4 controls on the page:
ObjectDataSource Controlo 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 theearlier step.
GridView Controlo This control is immediately under the "ObjectDataSource" control. This control
is bound to the "ObjectDataSource" control and is configured to Select, Update,and Delete.
-
8/9/2019 Microsoft Word - DAL+
15/26
LinkButton Controlo 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 tothe "ObjectDataSource" control and is configured to Insert.
A little more about the ObjectDataSource Control
Hover the mouse over the "ObjectDataSource" control until the small black arrow appears.
Click on it and the configuration menu will appear. click on "Configure Data Source".
The "ThingsForSaleController" class is configured as the Object Data Source.
Click the "Next >" button.
-
8/9/2019 Microsoft Word - DAL+
16/26
The next screen shows 4 tabs (Select, Update, Insert, and Delete). clicking on each tab revealsthat a different method of the "ThingsForSaleController" class is configured to handle eachfunction.
Click the "Next >" button.
-
8/9/2019 Microsoft Word - DAL+
17/26
The final screen shows the configuration for the parameter that is passed to the Select method.
Here we indicate "ModuleId" with a default value of "00". This is just a "place holder".
The actual value will be supplied at run-time by code in a later step.
-
8/9/2019 Microsoft Word - DAL+
18/26
In the properties window of the "ObjectDataSource" control we see that the"DataObjectTypeName" is set to our "ThingsForSaleInfo" class that was created in a previousstep.
This is how we indicate that this class will be used to pass data between the"ObjectDataSource" control and the "ThingsForSaleController" class.
-
8/9/2019 Microsoft Word - DAL+
19/26
Create the code behind
Double-click on the "ViewThingsForSale.ascx.vb" file so that it opens up in the designwindow.
Replace ALL the code with the following code: Imports DotNetNukeImports System.Web.UIImports System.Collections.GenericImports System.ReflectionImports DotNetNuke.Security.PortalSecurity
Namespace YourCompany.Modules.ThingsForSale
PartialClass ViewThingsForSale
Inherits Entities.Modules.PortalModuleBase
Dim ThingsForSaleInfo_data AsNew ThingsForSaleInfo
ProtectedSub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
IfIsInRole("Registered Users") Or IsInRole("Administrators") ThenAdd_My_Listing_LinkButton.Enabled = TrueElseAdd_My_Listing_LinkButton.Text = "You must be logged in to add a Listing"Add_My_Listing_LinkButton.Enabled = FalseEndIf
EndSub
ProtectedSub SetModuleId(ByVal sender AsObject, ByVal e AsSystem.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs)HandlesObjectDataSource_ThingsForSale.Selecting
e.InputParameters("ModuleId") = ModuleId.ToString
EndSub
ProtectedSub InsertingItem(ByVal sender AsObject, ByVal e AsSystem.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
e.Values.Item("UserID") = Entities.Users.UserController.GetCurrentUserInfo.UserIDe.Values.Item("ModuleId") = ModuleId.ToString()e.Values.Item("ID") = 0
-
8/9/2019 Microsoft Word - DAL+
20/26
EndSub
ProtectedSub InsertCancelButton_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
Me.FormView1.Visible = False
EndSub
ProtectedSub Add_My_Listing_LinkButton_Click(ByVal sender AsObject, ByVal e As System.EventArgs)Handles Add_My_Listing_LinkButton.Click
Me.FormView1.Visible = True
EndSub
ProtectedSub InsertButton_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
Me.FormView1.Visible = FalseAdd_My_Listing_LinkButton.Text = "Update Successful - Add Another Listing"
EndSub
ProtectedSub HideEditButtons(ByVal sender AsObject, ByVal e AsSystem.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Ife.Row.RowType = DataControlRowType.DataRow ThenThingsForSaleInfo_data = CType(e.Row.DataItem, ThingsForSaleInfo)
IfIsInRole("Administrators") Or (Entities.Users.UserController.GetCurrentUserInfo.UserID =CInt(ThingsForSaleInfo_data.UserID)) Thene.Row.Cells.Item(0).Enabled = TrueElsee.Row.Cells.Item(0).Text = ""EndIfEndIf
EndSub
EndClass
EndNamespace
We entered code that will handle:
-
8/9/2019 Microsoft Word - DAL+
21/26
Only allowing a person who is a registered user or an administrator to add an entry (IsInRole("Registered Users") OrIsInRole("Administrators") )Only allowing a person to edit their own entries(Entities.Users.UserController.GetCurrentUserInfo.UserID = CInt(ThingsForSaleInfo_data.UserID)Injecting the current "ModuleId" into the "ObjectDataSource" control.
Hopefully this will help those using Object Data Sources with DotNetNuke. There is a problemof 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 cant have them click a button on one
instance of the module and return data from another instance of the module.
You can see the solution in ViewThingsForSale.ascx and ViewThingsForSale.ascx.vb. In
the ViewThingsForSale.ascx file, in the "ObjectDataSource" control, we indicate the
ModuleId parameter as a Select parameter that will be passed to the Select method:
In ViewThingsForSale.ascx.vb we have this:
Protected Sub SetModuleId(ByVal sender As Object, ByVal e AsSystem.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) HandlesObjectDataSource_ThingsForSale.Selecting
e.InputParameters("ModuleId") = ModuleId.ToString
End Sub
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).
Compile and Build The Module
From the toolbar select "Build" then "Build Solution"
-
8/9/2019 Microsoft Word - DAL+
22/26
The site should build with no errors
In the SOLUTION EXPLORER right-click on the "Default.aspx" page and select SET AS
START PAGE
-
8/9/2019 Microsoft Word - DAL+
23/26
From the toolbar select DEBUG then START WITHOUT DEBUGGING
The website should now come up.
-
8/9/2019 Microsoft Word - DAL+
24/26
/** Create Table **/
CREATE TABLE {databaseOwner}[{objectQualifier}ThingsForSale] ([ID] [int] IDENTITY(1,1) NOT NULL,[ModuleId] [int] NOT NULL,
[UserID] [int] NULL,[Category] [nvarchar](25),[Description] [nvarchar](500),[Price] [float] NULL) ON [PRIMARY]
ALTER TABLE {databaseOwner}[{objectQualifier}ThingsForSale]ADDCONSTRAINT [PK_{objectQualifier}ThingsForSale] PRIMARY KEY CLUSTERED([ID]) ON [PRIMARY]
GO
-
8/9/2019 Microsoft Word - DAL+
25/26
/** Create Stored Procedures **/
CREATE PROCEDURE {databaseOwner}[{objectQualifier}ThingsForSale_Delete]
@ID intASDELETE FROM {objectQualifier}ThingsForSaleWHERE (ID = @ID)RETURNGO
CREATE PROCEDURE {databaseOwner}[{objectQualifier}ThingsForSale_Insert]@ModuleId int,@UserID int,@Category nvarchar(25),@Description nvarchar(500),@Price float
ASINSERT INTO {objectQualifier}ThingsForSale(ModuleId, UserID, Category, Description, Price)VALUES (@ModuleId,@UserID,@Category,@Description,@Price)RETURNGO
CREATE PROCEDURE {databaseOwner}[{objectQualifier}ThingsForSale_SelectAll]@ModuleId intASSELECT ID, ModuleId, UserID, Category, Description, PriceFROM {objectQualifier}ThingsForSaleWHERE (ModuleId = @ModuleId)
ORDER BY CategoryRETURNGO
CREATE PROCEDURE {databaseOwner}[{objectQualifier}ThingsForSale_Update]@ID int,@ModuleId int,@UserID int,@Category nvarchar(25),@Description nvarchar(500),@Price floatASUPDATE {objectQualifier}ThingsForSale
SET ModuleId = @ModuleId, UserID = @UserID, Category = @Category, Description = @Description, Price =@PriceWHERE (ID = @ID)RETURN
We ran a SQL script that created the table and the stored procedures.
You will notice that the script is written like:
-
8/9/2019 Microsoft Word - DAL+
26/26
CREATE TABLE {databaseOwner}[{objectQualifier}ThingsForSale]
rather than the normal:
CREATE TABLE [dbo][ThingsForSale]
The script commands "{databaseOwner}" and "{objectQualifier}" indicate that they are to bereplaced 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, ifalternate settings were indicated in the web.config file, those settings would be inserted into thescript.
You must have the "Run as Script" box checked for this replacement to happen.