 |
|
ขอคำสั่ง If หน่อยครับพอดีเขียน VB.Net Register ขึ่นมาแล้วมันไม่มี If สมัครไอดีซํ่าได้ขอบคุณครับ |
|
 |
|
|
 |
 |
|
ช่วยหน่อยนะคับพอดีผมเขียน VB.Net Register ขึ่นมาแล้วมันไม่มี If อะครับ มันเลยสมัครไอดีซํ่าได้
ผมต้องการ If 2-4 อย่างอะครับ
1.If เช็คไอดีว่ามีใครใช้ไปยัง
2.If กรอกข้อมูลให้ครบทุกช่อง เช่น TextBox1.Text กับ TextBox2.Text ต้องกรอกข้อมูลไว้ไม่งั่นจะมี MsgBox ขึ่นว่า "กรุณากรอกข้อมูลให้ครบ"
3.If กำหนดความสั่นความยาวของ Text ที่กรอกไป เช่น 6-20 ตัว
4.If ให้กรอกได้แค่ ภาษาอังกิดกับตัวเลขเท่านั้น
ขอบคุณครับ
นี้โค๊ตที่ผมเขียนขึ่นมา
Code (VB.NET)
Imports MySql.Data.MySqlClient
Public Class Form1
Dim ServerString As String = "Server=127.0.0.1;User Id=root;Password=test;Database=test"
Dim SQLConnection As MySqlConnection = New MySqlConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SQLConnection.ConnectionString = ServerString
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
MsgBox("ติดต่อฐานข้อมูลสำเร็จ !", MsgBoxStyle.Information)
Else
SQLConnection.Close()
MsgBox("ติดต่อฐานข้อมูลไม่ได้ !", MsgBoxStyle.Critical)
Me.Close()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SaveNames(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
MsgBox("สมัครสมาชิกสำเร็จ !", MsgBoxStyle.Information)
Me.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SQLStatement As String = "INSERT INTO user2 (username, password) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
SaveNames(SQLStatement)
End Sub
End Class
รบกวนด้วยนะครับ
Tag : .NET, MySQL, VB.NET, VS 2010 (.NET 4.x)
|
|
 |
 |
 |
 |
Date :
2011-12-08 15:28:36 |
By :
wanlop8822 |
View :
1478 |
Reply :
9 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เหมือนบทความนี้เลยครับ ให้เช็คก่อนครับ
Code (VB.NET)
Sub btnSave_Click(sender As Object, e As EventArgs)
Dim intNumRows As Integer
strSQL = "SELECT * FROM customer WHERE CustomerID = '"& Me.txtCustomerID.Text &"' "
objCmd = New MySqlCommand(strSQL, objConn)
intNumRows = objCmd.ExecuteScalar()
IF intNumRows > 0 Then
Me.pnlAdd.Visible = False
Me.lblStatus.Visible = True
Me.lblStatus.Text = "CustomerID already exist."
Else
strSQL = "INSERT INTO customer (CustomerID,Name,Email,CountryCode,Budget,Used) " & _
" VALUES " & _
" ('" & Me.txtCustomerID.Text & "','" & Me.txtName.Text & "','" & Me.txtEmail.Text & "', " & _
" '" & Me.txtCountryCode.Text & "','" & Me.txtBudget.Text & "','" & Me.txtUsed.Text & "')"
objCmd = New MySqlCommand
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
Me.pnlAdd.Visible = False
Try
objCmd.ExecuteNonQuery()
Me.lblStatus.Text = "Record Inserted"
Me.lblStatus.Visible = True
Catch ex As Exception
Me.lblStatus.Visible = True
Me.lblStatus.Text = "Record can not insert Error ("& ex.Message &")"
End Try
End IF
End Sub
Go to : ASP.NET MySQL Check Already Exists Add/Insert Record
|
 |
 |
 |
 |
Date :
2011-12-08 18:10:25 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ดูแล้ว งง อะครับมันเป็น aspx อะครับ ผมต้องการแบบ code เลยอะครับพี่วินช่วยผมหน่อยนะครับ
คือตอนนี้ขาดอย่างที่บอกเลย if เท่านั้นตรง
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As MySqlCommand = New MySqlCommand
Dim SQLStatement As String = "INSERT INTO user2 (username, password) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
SaveNames(SQLStatement)
End Sub
อะครับขาดแค่ If 2-4 อย่างเองพี่วินช่วยผมเติมหน่อยได้ไหมครับผม งง อะครับ
ผมต้องการ if 2-4 อย่าง เอง แต่เติมไม่เป็น
1.If เช็คไอดีว่ามีใครใช้ไปยัง
2.If กรอกข้อมูลให้ครบทุกช่อง เช่น TextBox1.Text กับ TextBox2.Text ต้องกรอกข้อมูลไว้ไม่งั่นจะมี MsgBox ขึ่นว่า "กรุณากรอกข้อมูลให้ครบ"
3.If กำหนดความสั่นความยาวของ Text ที่กรอกไป เช่น 6-20 ตัว
4.If ให้กรอกได้แค่ ภาษาอังกิดกับตัวเลขเท่านั้น
|
 |
 |
 |
 |
Date :
2011-12-09 01:08:34 |
By :
wanlop8822 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันก็ไม่ต่างอะไรกับ code ที่ผมใช้นี่ครับ
|
 |
 |
 |
 |
Date :
2011-12-09 06:46:14 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อิอิ ผมก็ไม่รู้เหมือนกันครับ ช่วยแปลเป็นโค๊ต VB แบบใส่เลยได้ไหมครับ ^^ ช่วยหน่อยนะครับพี่วิน
|
 |
 |
 |
 |
Date :
2011-12-09 09:51:29 |
By :
wanlop8822 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
พอดีผมเขียนเป็นโปรแกรมอะครับสมัครหน้าโปรแกรมเลย .exe ไม่ใช่ เป็น หน้าเว็บอะครับ
|
 |
 |
 |
 |
Date :
2011-12-09 11:28:43 |
By :
wanlop8822 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองแก้ไข function Savename ให้ return boolean
Code (VB.NET)
'อันนี้ผมลองเป็น SqlClient นะครับ
Public Function SaveNames(ByRef SQLStatement As String) As Boolean
Dim cmd As New SqlCommand
Dim tr As SqlTransaction
tr = SqlConnection.BeginTransaction()
Try
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SqlConnection
.Transaction = tr
.ExecuteNonQuery()
End With
tr.Commit()
Return True
Catch ex As Exception
tr.Rollback()
SqlConnection.Close()
Return False
End Try
End Function
' เรียกใช้ function SaveNames
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As MySqlCommand = New MySqlCommand
Dim SQLStatement As String = "INSERT INTO user2 (username, password) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "')"
'เรียกใช้ function SaveNames
If SaveNames(SQLStatement) = True Then
MsgBox("สมัครสมาชิกสำเร็จ !", MsgBoxStyle.Information, "บันทึกข้อมูล")
Else
MsgBox("สมัครสมาชิกไม่สำเร็จ !", MsgBoxStyle.Critical, "บันทึกข้อมูล")
End If
End Sub
|
ประวัติการแก้ไข 2011-12-09 14:31:21
 |
 |
 |
 |
Date :
2011-12-09 14:30:30 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไ่ม่ได้อะครับมันเออเร่อเลยเลย
|
 |
 |
 |
 |
Date :
2011-12-09 23:48:30 |
By :
wanlop8822 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. เช็คกรอกข้อมูลให้ครบทุกช่อง TextBox1.Text กับ TextBox2.Text
If Len(TextBox1.Text) <> 0 And Len(TextBox2.Text) <> 0 And Len(TextBox3.Text) <> 0 Then
End If
2. ให้เช็ค การกรอกได้แค่ ภาษาอังกิดกับตัวเลขเท่านั้น ก่อน
เขียนที่ textbox TextChanged และ กำหนดความสั่นความยาวของ Text ที่กรอกไป เช่น 6-20 ตัว
3 .If เช็คไอดีว่ามีใครใช้ไปยัง
|
 |
 |
 |
 |
Date :
2011-12-12 16:07:39 |
By :
edong |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณก็เปลีืยน Sqlcommand และ Sqltransaction ของ SqlClient
เป็นของ MySqlClient สิครับ ไม่เห็นที่ผมเมนต์ไว้หรอ ว่า เป็น SqlClient
|
 |
 |
 |
 |
Date :
2011-12-12 16:57:18 |
By :
tee |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|