제목 : 12. 수정 페이지 클래스 작성(Edit.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Public Class Edit
Inherits System.Web.UI.Page
#Region " Web Form 디자이너에서 생성한 코드 "
'이 호출은 Web Form 디자이너에 필요합니다.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Name As System.Web.UI.WebControls.TextBox
Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents Email As System.Web.UI.WebControls.TextBox
Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
Protected WithEvents Title As System.Web.UI.WebControls.TextBox
Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents Content As System.Web.UI.WebControls.TextBox
Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents btnSubmit 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 CStr(Request.UrlReferrer.ToString()) = "" 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("Select * From DotnetNoteBasicVB Where Num = @Num", objCon)
objCmd.Parameters.Add("@Num", SqlDbType.Int)
objCmd.Parameters("@Num").Value = Num
Try
objCon.Open()
objDr = objCmd.ExecuteReader()
If objDr.Read() Then
Name.Text = objDr.Item("Name")
Email.Text = objDr.Item("Email")
Title.Text = objDr.Item("Title")
Content.Text = objDr.Item("Content")
Else
lblError.Text = "글이 삭제되었거나 존재하지 않습니다.<br>"
End If
objDr.Close()
Catch ex As Exception
lblError.Text = "데이터 로딩에 실패하였습니다. 사유는 다음과 같습니다.<br>"
lblError.Text = ex.Message
Finally
objCon.Close()
End Try
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim objCon As SqlConnection
Dim objCmd As SqlCommand
objCon = New SqlConnection("server=localhost;user id=dotnetnote;password=dotnetnote;database=dotnetnote")
objCmd = New SqlCommand("Update DotnetNoteBasicVB Set Name=@Name, Email=@Email, Title=@Title, Content=@Content Where Num = @Num", objCon)
objCmd.Parameters.Add("@Name", SqlDbType.VarChar, 25)
objCmd.Parameters.Add("@Email", SqlDbType.VarChar, 100)
objCmd.Parameters.Add("@Title", SqlDbType.VarChar, 140)
objCmd.Parameters.Add("@Content", SqlDbType.Text)
objCmd.Parameters.Add("@Num", SqlDbType.Int)
objCmd.Parameters("@Name").Value = Name.Text
objCmd.Parameters("@Email").Value = Email.Text
objCmd.Parameters("@Title").Value = Title.Text
objCmd.Parameters("@Content").Value = Content.Text
objCmd.Parameters("@Num").Value = Num
Try
objCon.Open()
objCmd.ExecuteNonQuery()
Response.Redirect("List.aspx")
Catch ex As Exception
lblError.Text = "오류로 인해 저장이 실패하였습니다. 사유는 다음과 같습니다.<br>"
lblError.Text = ex.Message
Finally
objCon.Close()
End Try
End Sub
End Class