Hi,
I am new to DotNetNuke. I would like to delete a row from database based on a 'postID' and using a 'btnDeleteConfirm' button and using a stored procedure called 'STEM_Student_Opportunities_DeletePost'. See code below.
Here is the .ascx source file:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="aplViewSelectedOpp.ascx.vb" Inherits="Dana.DNN.APL.ViewOpp" %>
<table id="tblViewOpp" width="100%" runat="server" cellpadding="0" cellspacing="0">
<tr>
<td align="center" colspan="2">
<asp:Label ID="lblViewOpp" runat="server" Text="View Opportunity" Font-Bold="true" />
</td>
</tr>
<tr>
<td width="150px">
<asp:Label ID="lblDateAdded" Text="Date Added:" runat="server" />
</td>
<td>
<asp:Label ID="lblDateAddedMsg" runat="server" Width="100%"
style="text-align: justify" />
</td>
</tr>
<tr>
<td width="150px">
<asp:Label ID="lblUser" Text="User:" runat="server" />
</td>
<td>
<asp:Label ID="lblUserMsg" runat="server" Width="100%"
style="text-align: justify" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblOppName" Text="Opportunity Name:" runat="server" />
</td>
<td>
<asp:Label ID="lblOppNameMsg" runat="server" Width="100%" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblLinkField" Text="Link" runat="server" />
</td>
<td>
<asp:Label ID="lblLinkMsg" runat="server" Width="100%"
style="text-align: justify" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblNotesField" Text="Notes" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:Label ID="lblNotesMsg" runat="server" Width="100%" />
</td>
</tr>
<tr>
<td align="center" colspan="2">
<asp:Button ID="btnBack" Text="Back" runat="server" BackColor="AliceBlue"
Font-Bold="true" OnClick="btnBack_Click" />
<asp:Button ID="btnDelete" Text="Delete" runat="server" BackColor="Red"
Font-Bold="true" OnClick="btnDelete_Click" />
</td>
</tr>
</table>
<div id="deleteConfDiv" runat="server" style=" width: 100%; margin-left: 0;">
<br />
<asp:Label runat="server" ID="deleteConfLbl" Text="Are you sure you want to delete this post?" style="font-size: 20px; margin-left: 0; font-weight:bold; color:Black;" Font-Italic="true" ></asp:Label>
<br />
<br />
<asp:Button ID="btnDeleteConfirm" Text="Delete" runat="server" BackColor="Red" Font-Bold="true" OnClick="btnDeleteConfirm_Click" />
<asp:Button ID="btnBackConfrm" Text="Back" runat="server" BackColor="AliceBlue" Font-Bold="true" OnClick="btnBack_Click" style="text-align: center" />
</div>
***************************************************************
Here is the code-behind file:
Imports System.IO
Imports System.Web.UI.WebControls
Imports DotNetNuke.Entities.Users
Imports Dana.DNN.BLL
Namespace
Dana.DNN.APL
Partial Class ViewOpp
Inherits Entities.Modules.PortalModuleBase
#Region
"Private Members"
Private _postID As Integer
#End
Region
#Region
"Private Methods"
Private Sub ReadQueryString()
'Try to grab the PostID from the URL
If Not (Request.QueryString("PostID") Is Nothing) Then
_postID =
Int32.Parse(Request.QueryString("PostID"))
Else
_postID = -1
End If
End Sub
#End
Region
#Region
"Protected Methods"
Protected Function GetDateFormat(ByVal DatePosted As String) As String
'Convert the date into a neat format for display
If DateTime.Now.Date = DateTime.Parse(DatePosted).Date Then
Return "Today at " + DateTime.Parse(DatePosted).ToShortTimeString()
Else
If DateTime.Today.Date.AddDays(-1.0) = DateTime.Parse(DatePosted).Date Then
Return "Yesterday at " + DateTime.Parse(DatePosted).ToShortTimeString()
Else
Return DateTime.Parse(DatePosted).DayOfWeek().ToString() + ", " + DateTime.Parse(DatePosted).ToShortDateString() + " at " + DateTime.Parse(DatePosted).ToShortTimeString()
End If
End If
End Function
Protected Function GetUserName(ByVal UserID As Integer) As String
'Get the name of the user based on the userID
Dim objUserController As New UserController
Dim objUserInfo As New UserInfo
objUserInfo = objUserController.GetUser(
Me.PortalId, UserID)
If objUserInfo Is objUserController.GetUser(Me.PortalId, UserID) Then
Return objUserInfo.Username.ToString()
Else
Return "N/A"
End If
End Function
#End
Region
#Region
"Event Handlers"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
deleteConfDiv.Visible =
False
tblViewOpp.Visible =
True
ReadQueryString()
If (_postID = -1) Then
'This means that we couldn't grab the postID and therefore
'couldnt retrieve the message
lblNotesMsg.Text =
"Post Not Found"
Else
'Retrieve the message
Dim objPostController As New PostController
Dim objPostInfo As New PostInfo
objPostInfo = objPostController.Get(_postID)
'If (objPostInfo) Then
lblDateAddedMsg.Text = GetDateFormat(objPostInfo.DateAdded.ToString())
lblUserMsg.Text = GetUserName(objPostInfo.UserID)
lblOppNameMsg.Text = objPostInfo.OppName
lblLinkMsg.Text = objPostInfo.Link
lblNotesMsg.Text = objPostInfo.Notes
'Allow the user who made the post, or an admin, or the host to be able to delete it
If ((Me.UserInfo.IsInRole("Admin")) Or (Me.UserInfo.IsSuperUser) Or (Me.UserId = objPostInfo.UserID)) Then
btnDelete.Visible =
True
Else
btnDelete.Visible =
False
End If
'Else
'Post ID is not in the database
' lblNotesMsg.Text = "Post Not Found"
End If
End If
' End If
End Sub
Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Got back to the Posts
Response.Redirect(NavigateURL(
Me.TabId, ""), True)
End Sub
Protected Sub btnDelete_Click()
'Brings to the confirmation page once the btnDelete is clicked.
deleteConfDiv.Visible =
True
tblViewOpp.Visible =
False
End Sub
Protected Sub btnDeleteConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'Delete the message using the postID
Dim objPostController As New PostController
objPostController.Delete(_postID)
Response.Redirect(NavigateURL(
Me.TabId, ""), True)
End Sub
#End
Region
End Class
End
Namespace
******************************
Here is the Controller.vb file:
Imports
System
Imports
Dana.DNN.DAL
Namespace
Dana.DNN.BLL
Public Class PostController
Implements Entities.Modules.IPortable
#Region
"Public Methods"
Public Function [Get](ByVal PostID As Integer) As PostInfo
Return CType(CBO.FillObject(DataProvider.Instance().Get(PostID), GetType(PostInfo)), PostInfo)
End Function
Public Function List() As ArrayList
Return CBO.FillCollection(DataProvider.Instance().List(), GetType(PostInfo))
End Function
Public Function ListByUser(ByVal UserID As Integer) As ArrayList
Return CBO.FillCollection(DataProvider.Instance().ListByUser(UserID), GetType(PostInfo))
End Function
Public Function Search(ByVal OppName As String) As ArrayList
Return CBO.FillCollection(DataProvider.Instance().Search(OppName), GetType(PostInfo))
End Function
Public Function Add(ByVal objPostInfo As PostInfo) As Integer
Return CType(DataProvider.Instance().Add(objPostInfo.UserID, objPostInfo.OppName, objPostInfo.Link, objPostInfo.Notes, objPostInfo.DateAdded), Integer)
End Function
Public Sub Delete(ByVal PostID As Integer)
DataProvider.Instance().Delete(PostID)
End Sub
Public Sub DeleteOldPosts(ByVal DateAdded As Date)
DataProvider.Instance().DeleteOldPosts(DateAdded)
End Sub
#End
Region
#Region
"Stuff We Left Blank"
Public Function ExportModule(ByVal ModuleID As Integer) As String Implements DotNetNuke.Entities.Modules.IPortable.ExportModule
End Function
Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserID As Integer) Implements DotNetNuke.Entities.Modules.IPortable.ImportModule
End Sub
#End
Region
End Class
End
Namespace
***************************************
Here is the Info.vb file:
Imports
System
Namespace
Dana.DNN.BLL
Public Class PostInfo
#Region
"Private Members"
Private _postID As Integer
Private _userID As Integer
Private _OppName As String
Private _Link As String
Private _Notes As String
Private _dateAdded As DateTime
#End
Region
#Region
"Shared Public Properties"
Public Property PostID() As Integer
Get
Return _postID
End Get
Set(ByVal Value As Integer)
_postID = Value
End Set
End Property
Public Property UserID() As Integer
Get
Return _userID
End Get
Set(ByVal Value As Integer)
_userID = Value
End Set
End Property
Public Property OppName() As String
Get
Return _OppName
End Get
Set(ByVal Value As String)
_OppName = Value
End Set
End Property
Public Property Link() As String
Get
Return _Link
End Get
Set(ByVal Value As String)
_Link = Value
End Set
End Property
Public Property Notes() As String
Get
Return _Notes
End Get
Set(ByVal Value As String)
_Notes = Value
End Set
End Property
Public Property DateAdded() As DateTime
Get
Return _dateAdded
End Get
Set(ByVal Value As DateTime)
_dateAdded = Value
End Set
End Property
#End
Region
End Class
End
Namespace
**************************
Here is the SqlDataProvider file:
/************************************************************/
/***** SqlDataProvider *****/
/************************************************************/
/* =====================================================================================
/ Tables
/ ===================================================================================== */
/*********************** Create Table:STEM_Student_Opportunities ****************************/
IF NOT EXISTS (SELECT * FROM {databaseOwner}sysobjects WHERE id = object_id(N'{databaseOwner}[{objectQualifier}STEM_Student_Opportunities]') and OBJECTPROPERTY(id, N'IsTable') = 1)
BEGIN
CREATE TABLE {databaseOwner}[{objectQualifier}STEM_Student_Opportunities]
(
[PostID] INT NOT NULL, IDENTITY(1,1),
[UserID] INT NOT NULL,
[DateAdded] DATETIME NOT NULL,
[OppName] NVARCHAR(255) NOT NULL,
[Link] NVARCHAR(1000) NOT NULL,
[Notes] NVARCHAR(255)
)
ALTER TABLE {databaseOwner}[{objectQualifier}STEM_Student_Opportunities] WITH NOCHECK ADD
CONSTRAINT [PK_{objectQualifier}STEM_Student_Opportunities] PRIMARY KEY (PostID)
END
GO
/* =====================================================================================
/ Stored Procedures
/ ===================================================================================== */
/* ========================== Student Opportunities ================================== */
/*********************** Drop Existing Stored Procedures ****************************/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_GetPost') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_GetPost
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPosts') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPosts
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPostsByUser') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPostsByUser
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_SearchPosts') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_SearchPosts
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_AddPost') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_AddPost
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeletePost') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeletePost
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'{databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeleteOldPosts') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeleteOldPosts
GO
/*********************** Create Stored Procedures ****************************/
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_GetPost
@PostID int
AS
SELECT *
FROM
{objectQualifier}STEM_Student_Opportunities
WHERE
([PostID] = @PostID)
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPosts
AS
SELECT *
FROM
{objectQualifier}STEM_Student_Opportunities
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_ListPostsByUser
@UserID int
AS
SELECT *
FROM
{objectQualifier}STEM_Student_Opportunities
WHERE
([UserID] = @UserID)
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_SearchPosts
@OppName nvarchar(255)
AS
SELECT *
FROM
{objectQualifier}STEM_Student_Opportunities
WHERE
([OppName] LIKE @OppName)
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_AddPost
@UserID int,
@OppName nvarchar(255),
@Link nvarchar(1000),
@Notes nvarchar(255),
@DateAdded datetime
AS
INSERT INTO {objectQualifier}STEM_Student_Opportunities
(
[UserID],
[OppName],
[Link],
[Notes],
[DateAdded]
)
VALUES
(
@UserID,
@OppName,
@Link,
@Notes,
@DateAdded
)
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeletePost
@PostID int
AS
SELECT * FROM {objectQualifier}STEM_Student_Opportunities
WHERE
(PostID] = @PostID)
GO
CREATE PROCEDURE {databaseOwner}{objectQualifier}STEM_Student_Opportunities_DeleteOldPosts
@DateAdded datetime
AS
DELETE FROM {objectQualifier}STEM_Student_Opportunities
WHERE
([DateAdded] < @DateAdded)
GO
/************************************************************/
/***** SqlDataProvider *****/
/************************************************************/