Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Hello I have a registration form mysql need to add code to detect if the user exists. The existence if it should show the window (alredy registered user)
code:



Imports MySql.Data.MySqlClient
Public Class Form2

'create a public function and set your MySQL Connection .
Public Function mysqlconnection() As MySqlConnection
'return new connection.
Return New MySqlConnection("Server=xxx;User ID=xx;Password=xx;Database=xx")
End Function

'pass the value of mysqlconnection() as MySQL connection to con.
Public con As MySqlConnection = MySqlConnection()

'declaring the variables string
Public sql As String
Public result As String

'declaring the classes
Dim cmd As New MySqlCommand
Public dt As New DataTable
Public da As New MySqlDataAdapter


Public Sub create(ByVal sql As String)

Try
'open the connection
con.Open()
'set your command
With cmd
'holds the data to be executed
.Connection = con
.CommandText = sql
'execute the data
result = cmd.ExecuteNonQuery

'coditioning the result whether succesfull or failed when it is executed.
If result = 0 Then
'the execution of data is failed
MsgBox("Registration failed!")
Else
'the executed data is succesfull
MsgBox("You are now registered.")
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub


Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click


'put the string value to sql
sql = "INSERT INTO `users` (`email`, `username`, `Pass` ) VALUES ('" _
& txtemail.Text & "','" & txtuser.Text & "','" & txtpass.Text & "')"

'call your sub name and put the sql in the parameters list.
create(sql)

End Sub





Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub
End Class
Posted

1 solution

Try this to check duplicate username before inserting.
VB
sqlcommand = New SqlCommand("Select * from users where username=yourusername", cnn)
Dim adp As SqlDataAdapter = New SqlDataAdapter(sqlcommand)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
If ds.Tables(0).Rows.Count > 0 Then
Return True
End If
Return False
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900