제목 : 5. 입력 페이지 클래스 작성(Insert.aspx.vb)
Imports System.Data
Imports System.Data.SqlClient
Public Class Insert
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 RequiredFieldValidator4 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
Protected WithEvents Password As System.Web.UI.WebControls.TextBox
'참고: 다음의 자리 표시자 선언은 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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'여기에 사용자 코드를 배치하여 페이지를 초기화합니다.
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim objCon As New SqlConnection
Dim objCmd As New SqlCommand
objCon = New SqlConnection("server=localhost;user id=dotnetnote;password=dotnetnote;database=dotnetnote")
'objCon.Open()
objCmd = New SqlCommand("Insert DotnetNoteBasicVB (Name, Email, Title, PostDate, ReadCount," & _
" PostIP, Password, Content) Values (@Name, @Email, @Title, @PostDate, @ReadCount," & _
" @PostIP, @Password, @Content)", 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("@PostDate", SqlDbType.DateTime)
objCmd.Parameters.Add("@ReadCount", SqlDbType.Int)
objCmd.Parameters.Add("@PostIP", SqlDbType.VarChar, 15)
objCmd.Parameters.Add("@Password", SqlDbType.VarChar, 20)
objCmd.Parameters.Add("@Content", SqlDbType.Text)
objCmd.Parameters("@Name").Value = Name.Text
objCmd.Parameters("@Email").Value = Email.Text
objCmd.Parameters("@Title").Value = Title.Text
objCmd.Parameters("@PostDate").Value = DateTime.Now
objCmd.Parameters("@ReadCount").Value = 0
objCmd.Parameters("@PostIP").Value = Request.ServerVariables("REMOTE_HOST")
objCmd.Parameters("@Password").Value = Password.Text
objCmd.Parameters("@Content").Value = Content.Text
'objCmd.ExecuteNonQuery()
'objCon.Close()
Try
objCon.Open()
objCmd.ExecuteNonQuery()
lblError.Text = "글이 저장되었습니다."
Catch ex As Exception
lblError.Text = "오류로 인해 저장이 실패하였습니다. 사유는 다음과 같습니다.<br>"
lblError.Text = ex.Message
Finally
objCon.Close()
End Try
End Sub
End Class