제목 : 10. 세부 출력/삭제 페이지 클래스 작성(Content.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Public Class Content
Inherits System.Web.UI.Page
#Region " Web Form 디자이너에서 생성한 코드 "
'이 호출은 Web Form 디자이너에 필요합니다.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents lblName As System.Web.UI.WebControls.Label
Protected WithEvents lblEmail As System.Web.UI.WebControls.Label
Protected WithEvents lblPostDate As System.Web.UI.WebControls.Label
Protected WithEvents lblReadCount As System.Web.UI.WebControls.Label
Protected WithEvents lblTitle As System.Web.UI.WebControls.Label
Protected WithEvents lblContent As System.Web.UI.WebControls.Label
Protected WithEvents txtPassword As System.Web.UI.WebControls.TextBox
Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents btnEdit As System.Web.UI.WebControls.Button
Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
Protected WithEvents lblError As System.Web.UI.WebControls.Label
Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
'참고: 다음의 자리 표시자 선언은 Web Form 디자이너의 필수 선언입니다.
'삭제하거나 옮기지 마십시오.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 이 메서드 호출은 Web Form 디자이너에 필요합니다.
'코드 편집기를 사용하여 수정하지 마십시오.
InitializeComponent()
End Sub
#End Region
Dim Num As String = ""
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'여기에 사용자 코드를 배치하여 페이지를 초기화합니다.
Num = Request.QueryString("Num")
If Num = "" Then
Response.Redirect("List.aspx")
End If
If Not IsPostBack Then
LoadData()
End If
End Sub
Private Sub LoadData()
Dim objCon As SqlConnection
Dim objCmd As SqlCommand
Dim objDr As SqlDataReader
objCon = New SqlConnection("server=localhost;user id=dotnetnote;password=dotnetnote;database=dotnetnote")
objCon.Open()
objCmd = New SqlCommand("Update DotnetNoteBasicVB Set ReadCount = ReadCount + 1 Where Num = @Num", objCon)
objCmd.Parameters.Add("@Num", SqlDbType.Int)
objCmd.Parameters("@Num").Value = Num
objCmd.ExecuteNonQuery()
objCmd.CommandText = "Select * From DotnetNoteBasicVB Where Num = @Num"
'objCmd.Parameters.Add("@Num", SqlDbType.Int)
'objCmd.Parameters("@Num").Value = Num
objDr = objCmd.ExecuteReader
If objDr.Read() Then
lblName.Text = objDr.Item("Name")
lblEmail.Text = objDr.Item("Email")
lblPostDate.Text = objDr.Item("PostDate")
lblTitle.Text = objDr.Item("Title")
lblContent.Text = Replace(objDr.Item("Content"), vbCrLf, "<br>")
Else
lblError.Text = "글이 삭제되었거나 존재하지 않습니다.<br>"
End If
objDr.Close()
objCon.Close()
End Sub
Private Function IsPasswordValid()
Dim blnFlag As Boolean = False
Dim objCon As SqlConnection
Dim objCmd As SqlCommand
objCon = New SqlConnection("server=localhost;user id=dotnetnote;password=dotnetnote;database=dotnetnote")
'objCon.Open()
objCmd = New SqlCommand("Select Password From DotnetNoteBasicVB Where Num = @Num", objCon)
objCmd.Parameters.Add("@Num", SqlDbType.Int)
objCmd.Parameters("@Num").Value = Num
Try
objCon.Open()
Dim Password As String
Password = objCmd.ExecuteScalar()
If Password = txtPassword.Text Then
blnFlag = True
Else
blnFlag = False
End If
Catch ex As Exception
lblError.Text = "예외가 발생하였습니다. 사유는 다음과 같습니다.<br>"
lblError.Text = ex.Message
Finally
objCon.Close()
End Try
Return blnFlag
End Function
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If Not IsPasswordValid() Then
lblError.Text = "비밀번호가 일치하지 않습니다. -_-+"
Else
Response.Redirect("Edit.aspx?Num=" & Num)
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If Not IsPasswordValid() Then
lblError.Text = "비밀번호가 일치하지 않습니다. -_-+"
Else
DeleteRecord()
Response.Redirect("List.aspx")
End If
End Sub
Private Sub DeleteRecord()
Dim objCon As SqlConnection
Dim objCmd As SqlCommand
objCon = New SqlConnection("server=localhost;user id=dotnetnote;password=dotnetnote;database=dotnetnote")
'objCon.Open()
Try
objCon.Open()
objCmd = New SqlCommand("Delete From DotnetNoteBasicVB Where Num = @Num", objCon)
objCmd.Parameters.Add("@Num", SqlDbType.Int)
objCmd.Parameters("@Num").Value = Num
objCmd.ExecuteNonQuery()
Catch ex As Exception
lblError.Text = "예외가 발생하였습니다. 사유는 다음과 같습니다.<br>"
lblError.Text = ex.Message
Finally
objCon.Close()
End Try
End Sub
End Class