Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub BindData()
Dim strConnString As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
objConn = New Data.SqlClient.SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "INSERT INTO Username (Name,Surname,Username,Password,ConfirmPassword,Email)" & _
" VALUES ('" & txtName.Text & "','" & txtSurname.Text & "','" & txtUsername.Text & "' " & _
" ,'" & txtPassword.Text & "','" & txtConfirmPassword.Text & "','" & txtEmail.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
Response.Write("language=javascript>if(window.alert('บันทึกสำเร็จ'))")
Catch ex As Exception
Response.Write("language=javascript>if(window.alert('บันทึกไม่สำเร็จ'))")
End Try
objConn.Close()
objConn = Nothing
End Sub
Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
BindData()
End Sub
ทำไมถึงไม่แสดงแค่ "บันทึกสำเร็จ" อ่ะคะ
Tag : .NET, Ms SQL Server 2005, Web (ASP.NET), VB.NET
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections
Imports System.Text
''' <summary>
''' Summary description for MessageBox
''' </summary>
Public Class MessageBox
Private Shared m_executingPages As New Hashtable()
'
' TODO: Add constructor logic here
'
Public Sub New()
End Sub
Public Shared Sub Show(sMessage As String)
' If this is the first time a page has called this method then
If Not m_executingPages.Contains(HttpContext.Current.Handler) Then
' Attempt to cast HttpHandler as a Page.
Dim executingPage As Page = TryCast(HttpContext.Current.Handler, Page)
If executingPage IsNot Nothing Then
' Create a Queue to hold one or more messages.
Dim messageQueue As New Queue()
' Add our message to the Queue
messageQueue.Enqueue(sMessage)
' Add our message queue to the hash table. Use our page reference
' (IHttpHandler) as the key.
m_executingPages.Add(HttpContext.Current.Handler, messageQueue)
' Wire up Unload event so that we can inject
' some JavaScript for the alerts.
AddHandler executingPage.Unload, New EventHandler(AddressOf ExecutingPage_Unload)
End If
Else
' If were here then the method has allready been
' called from the executing Page.
' We have allready created a message queue and stored a
' reference to it in our hastable.
Dim queue As Queue = DirectCast(m_executingPages(HttpContext.Current.Handler), Queue)
' Add our message to the Queue
queue.Enqueue(sMessage)
End If
End Sub
' Our page has finished rendering so lets output the
' JavaScript to produce the alert's
Private Shared Sub ExecutingPage_Unload(sender As Object, e As EventArgs)
' Get our message queue from the hashtable
Dim queue As Queue = DirectCast(m_executingPages(HttpContext.Current.Handler), Queue)
If queue IsNot Nothing Then
Dim sb As New StringBuilder()
' How many messages have been registered?
Dim iMsgCount As Integer = queue.Count
' Use StringBuilder to build up our client slide JavaScript.
sb.Append("<script language='javascript'>")
' Loop round registered messages
Dim sMsg As String
While System.Math.Max(System.Threading.Interlocked.Decrement(iMsgCount),iMsgCount + 1) > 0
sMsg = DirectCast(queue.Dequeue(), String)
sMsg = sMsg.Replace(vbLf, "\n")
sMsg = sMsg.Replace("""", "'")
sb.Append("alert( """ & sMsg & """ );")
End While
' Close our JS
sb.Append("</script>")
' Were done, so remove our page reference from the hashtable
m_executingPages.Remove(HttpContext.Current.Handler)
' Write the JavaScript to the end of the response stream.
HttpContext.Current.Response.Write(sb.ToString())
End If
End Sub
End Class
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub BindData()
Dim strConnString As String
strConnString = "Server=localhost\SQLExpress;Database=Myweb;Trusted_Connection=True;"
objConn = New Data.SqlClient.SqlConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "INSERT INTO Username (Name,Surname,Username,Password,ConfirmPassword,Email)" & _
" VALUES ('" & txtName.Text & "','" & txtSurname.Text & "','" & txtUsername.Text & "' " & _
" ,'" & txtPassword.Text & "','" & txtConfirmPassword.Text & "','" & txtEmail.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Try
objCmd.ExecuteNonQuery()
MsgBox("บันทึกสำเร็จ", MsgBoxStyle.OkOnly, "Complete")
Catch ex As Exception
MsgBox("บันทึกไม่สำเร็จ " & ex.Message.ToString(), MsgBoxStyle.OkOnly, "Fail")
End Try
End Sub
Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As System.EventArgs)
BindData()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
แล้วถ้าเราจะตั้งว่ากด ok จาก MsgBox แล้วให้กลับไปหน้า Home จะทำยังไงอ่ะคะ
Dim response As MsgBoxResult
response = MsgBox("บันทึกสำเร็จ", MsgBoxStyle.OkOnly, "Complete")
If response = MsgBoxResult.OK Then ' User chose OK.
' Perform some action.
Response.ReDirect("xxxxxx.aspx")
Else
' Perform some other action.
End If