-
Jun 23rd, 2015, 08:43 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Check Name does it exist first
How to make this code
If Name exist in database to display messagebox (Account name already exist try another)
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myconnection As SqlConnection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
myconnection.Open()
Using mycommand As SqlCommand = myconnection.CreateCommand
mycommand.CommandText = "INSERT INTO MEMB_INFO (memb___id, memb__pwd, memb_name, sno__numb, mail_addr,bloc_code,ctl1_code) VALUES (@nAccName, @nPass, @nAccName, @nIDCode, @nEmail, 0, 1)"
mycommand.Parameters.AddWithValue("@nAccName", TextBox2.Text)
mycommand.Parameters.AddWithValue("@nPass", TextBox3.Text)
mycommand.Parameters.AddWithValue("@nIDCode", TextBox5.Text)
mycommand.Parameters.AddWithValue("@nEmail", TextBox4.Text)
Dim rowsAffected As Integer = mycommand.ExecuteNonQuery()
' This would be one always in this case unless the statement failed
MessageBox.Show("Information Saved")
End Using
myconnection.Close()
End Using
End Sub
Because now create the new account but make dublicate account
Example in database i have account with name "User"
And i create new account with name "User" and he insert the new account and in database has now 2 accounts with "User"
-
Jun 23rd, 2015, 09:01 AM
#2
Re: Check Name does it exist first
You can use an SQL command to return the amount of rows that have the same username. If it's greater than zero, then don't insert the row. Here is a quick example that will return if there is already a row that has the same value in memb_name:
Code:
Private Function DuplicateUser(ByVal username As String) As Boolean
Dim connection As SqlConenction = Nothing
Dim duplicate As Boolean = True
Try
connection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand("SELECT COUNT(memb_name) FROM MEMB_INFO WHERE memb_name = @name")
cmd.Parameters.AddWithValue("@name", username)
duplicate = Convert.ToInt32(cmd.ExecuteScalar) > 0
connection.Close()
Catch ex As Exception
duplicate = True
Console.WriteLine(ex.Message)
Finally
If connection IsNot Nothing Then
If connection.State = ConnectionState.Open Then
connection.Close()
End If
connection.Dispose()
End If
End Try
Return duplicate
End Function
Last edited by dday9; Jun 23rd, 2015 at 09:16 AM.
-
Jun 23rd, 2015, 09:17 AM
#3
Thread Starter
Fanatic Member
Re: Check Name does it exist first
ah i can't manage it with your code maybe i make it wrong, can you tell me how should it become in my code:
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myconnection As SqlConnection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
myconnection.Open()
If TextBox2.Text = "" Then
MessageBox.Show("Please Enter Name", "Error")
ElseIf TextBox3.Text = "" Then
MessageBox.Show("Please Enter Password", "Error")
ElseIf TextBox4.Text = "" Then
MessageBox.Show("Please Enter Email", "Error")
ElseIf TextBox5.Text = "" Then
MessageBox.Show("Please Enter PersonalID", "Error")
Else
Using mycommand As SqlCommand = myconnection.CreateCommand
mycommand.CommandText = "INSERT INTO MEMB_INFO (memb___id, memb__pwd, memb_name, sno__numb, mail_addr,bloc_code,ctl1_code) VALUES (@nAccName, @nPass, @nAccName, @nIDCode, @nEmail, 0, 1)"
mycommand.Parameters.AddWithValue("@nAccName", TextBox2.Text)
mycommand.Parameters.AddWithValue("@nPass", TextBox3.Text)
mycommand.Parameters.AddWithValue("@nIDCode", TextBox5.Text)
mycommand.Parameters.AddWithValue("@nEmail", TextBox4.Text)
Dim rowsAffected As Integer = mycommand.ExecuteNonQuery()
' This would be one always in this case unless the statement failed
MessageBox.Show("Information Saved", "Sucsess")
End Using
myconnection.Close()
End If
End Using
End Sub
-
Jun 23rd, 2015, 09:21 AM
#4
Re: Check Name does it exist first
You would use a conditional statement to check if the value returned by the function is False, if it is then continue on with your code.
-
Jun 23rd, 2015, 09:25 AM
#5
Thread Starter
Fanatic Member
Re: Check Name does it exist first
i try it with your example but i got a lot of errors cause not match
i'm not sure how should it become if i add after Else check for empty fields
can you add the missing part with red in my code so i can continued with code thanks
-
Jun 23rd, 2015, 09:54 AM
#6
Re: Check Name does it exist first
I'm not in the mood to spoon feed you. You have all the information in front of you, if you do not know how to create a conditional statement to check if the value returned by the function is False then you have more things to worry about.
-
Jun 23rd, 2015, 10:21 AM
#7
Thread Starter
Fanatic Member
Re: Check Name does it exist first
thats why i post here, for help omg..
ok so maybe someone else would help me how to add it in my code..
-
Jun 24th, 2015, 01:59 AM
#8
Thread Starter
Fanatic Member
Re: Check Name does it exist first
-
Jun 25th, 2015, 09:00 AM
#9
Thread Starter
Fanatic Member
Re: Check Name does it exist first
-
Jun 25th, 2015, 10:16 AM
#10
Re: Check Name does it exist first
What have you done to integrate the code? Copy into your app, call the method: if it returns true then there is a duplicate user.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jun 25th, 2015, 12:49 PM
#11
Re: Check Name does it exist first
To use dday9 method you need to add his code to your app and then,
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myconnection As SqlConnection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
myconnection.Open()
If TextBox2.Text = "" Then
MessageBox.Show("Please Enter Name", "Error")
ElseIf TextBox3.Text = "" Then
MessageBox.Show("Please Enter Password", "Error")
ElseIf TextBox4.Text = "" Then
MessageBox.Show("Please Enter Email", "Error")
ElseIf TextBox5.Text = "" Then
MessageBox.Show("Please Enter PersonalID", "Error")
Else
'CHECK IF NAME EXISTS
If Not DuplicateUser(nAccName) Then
Using mycommand As SqlCommand = myconnection.CreateCommand
mycommand.CommandText = "INSERT INTO MEMB_INFO (memb___id, memb__pwd, memb_name, sno__numb, mail_addr,bloc_code,ctl1_code) VALUES (@nAccName, @nPass, @nAccName, @nIDCode, @nEmail, 0, 1)"
mycommand.Parameters.AddWithValue("@nAccName", TextBox2.Text)
mycommand.Parameters.AddWithValue("@nPass", TextBox3.Text)
mycommand.Parameters.AddWithValue("@nIDCode", TextBox5.Text)
mycommand.Parameters.AddWithValue("@nEmail", TextBox4.Text)
Dim rowsAffected As Integer = mycommand.ExecuteNonQuery()
' This would be one always in this case unless the statement failed
MessageBox.Show("Information Saved", "Sucsess")
End Using
myconnection.Close()
Else
MessageBox.Show("User Name Already Exists")
End If
End If
End Using
End Sub
If "mem_name" is meant to be unique or primarykey then this would work fine. But if "mem_id" or some other field is the primarykey then you would need to adjust DuplicateUser and the parameter you send to the function accordingly.
-
Jun 26th, 2015, 02:59 AM
#12
Thread Starter
Fanatic Member
Re: Check Name does it exist first
what it means: If Not DuplicateUser(nAccName) Then
what i need to replace there i try with Textbox2.text(@nAccName) but its underlined
-
Jun 26th, 2015, 11:50 AM
#13
Re: Check Name does it exist first
what it means: If Not DuplicateUser(nAccName) Then
Your sending nAccName to the function that dday9 wrote for you, which returns either True of False.
After looking at your code again, it should be,
Code:
If Not DuplicateUser(Textbox2.Text) Then
-
Jun 26th, 2015, 12:12 PM
#14
Thread Starter
Fanatic Member
Re: Check Name does it exist first
i got underline in DuplicateUser
-
Jun 26th, 2015, 12:31 PM
#15
Re: Check Name does it exist first
Originally Posted by diablo21
i got underline in DuplicateUser
That is because you are not implementing the code that I provided.
-
Jun 26th, 2015, 12:58 PM
#16
Re: Check Name does it exist first
Did you add the function to your project?
-
Jun 26th, 2015, 01:07 PM
#17
Thread Starter
Fanatic Member
Re: Check Name does it exist first
yes but i got underline about this
aa maybe i forgot add : Dim duplicate As Boolean = True
edit nope..not from this maybe its because i use button ?
Last edited by diablo21; Jun 26th, 2015 at 01:14 PM.
-
Jun 26th, 2015, 01:23 PM
#18
Re: Check Name does it exist first
Where did you put the function dday9 provided?
You can't call it in your code if you didn't add it to your code. I just put it after the Button2_Click Sub and there is no underline under the call. {p.s. did have to fix a misspelling and add an End Using, but little things like that are to be expected with code written ad-hoc in the forum}.
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Using myconnection As SqlConnection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
myconnection.Open()
If TextBox2.Text = "" Then
MessageBox.Show("Please Enter Name", "Error")
ElseIf TextBox3.Text = "" Then
MessageBox.Show("Please Enter Password", "Error")
ElseIf TextBox4.Text = "" Then
MessageBox.Show("Please Enter Email", "Error")
ElseIf TextBox5.Text = "" Then
MessageBox.Show("Please Enter PersonalID", "Error")
Else
'CHECK IF NAME EXISTS
If Not DuplicateUser(TextBox2.Text) Then
Using mycommand As SqlCommand = myconnection.CreateCommand
mycommand.CommandText = "INSERT INTO MEMB_INFO (memb___id, memb__pwd, memb_name, sno__numb, mail_addr,bloc_code,ctl1_code) VALUES (@nAccName, @nPass, @nAccName, @nIDCode, @nEmail, 0, 1)"
mycommand.Parameters.AddWithValue("@nAccName", TextBox2.Text)
mycommand.Parameters.AddWithValue("@nPass", TextBox3.Text)
mycommand.Parameters.AddWithValue("@nIDCode", TextBox5.Text)
mycommand.Parameters.AddWithValue("@nEmail", TextBox4.Text)
Dim rowsAffected As Integer = mycommand.ExecuteNonQuery()
' This would be one always in this case unless the statement failed
MessageBox.Show("Information Saved", "Sucsess")
End Using
myconnection.Close()
Else
MessageBox.Show("User Name Already Exists")
End If
End If
End Using
End Sub
Private Function DuplicateUser(ByVal username As String) As Boolean
Dim connection As SqlConnection = Nothing
Dim duplicate As Boolean = True
Try
connection = New SqlConnection("Server=127.0.0.1;Database=MuOnline;Integrated Security=True")
Using cmd As SqlCommand = New SqlCommand("SELECT COUNT(memb_name) FROM MEMB_INFO WHERE memb_name = @name")
cmd.Parameters.AddWithValue("@name", username)
duplicate = Convert.ToInt32(cmd.ExecuteScalar) > 0
connection.Close()
End Using
Catch ex As Exception
duplicate = True
Console.WriteLine(ex.Message)
Finally
If connection IsNot Nothing Then
If connection.State = ConnectionState.Open Then
connection.Close()
End If
connection.Dispose()
End If
End Try
Return duplicate
End Function
-
Jun 26th, 2015, 01:43 PM
#19
Thread Starter
Fanatic Member
Re: Check Name does it exist first
aa now i understand it how it should be thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|