Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm still new to this so please help. i have no idea how to prevent data from duplicating. this is my code.

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addbtn.Click

    If myconn.State = ConnectionState.Open Then
        myconn.Close()
    End If
    
    myconn.Open()
    Dim i As Integer = mycom.ExecuteNonQuery()
    Dim Q As String
    ''-------------------------------------------------------------------------------------
    ''-------------------------------------------------------------------------------------
    
    Q = "INSERT INTO tblelogin(Username,name,Password,Accounttype) VALUES('"
    Q = Q + txtuname.Text + "','" + txtname.Text + "','" + txtpass.Text + "','" + accountbox.Text + "')"
    
    If txtpass.Text <> txtconfirm.Text Then
        MsgBox("password mismatch. please enter again.")
        txtpass.Text = ""
        txtconfirm.Text = ""
    
    End If
    
    If txtuname.Text = "" Or _
        txtpass.Text = "" Or _
        txtname.Text = "" Or _
        txtconfirm.Text = "" Or _
        accountbox.Text = "" Then
        MsgBox("input data on the field.")
        
        Exit Sub
    Else
    
        mycom.CommandText = Q
        
        i = mycom.ExecuteNonQuery()
        If (i > 0) Then
            MessageBox.Show("Record is Successfully Inserted")
        Else
            MessageBox.Show("Record is not Inserted")
            
            myconn.Close()
        
        End If
    End If
Posted
Updated 13-Aug-13 22:59pm
v2
Comments
Maarten Kools 14-Aug-13 5:01am    
You're always doing and INSERT now. You'll have to check if an entry already exists, and if it does, perform an UPDATE; otherwise, perform the INSERT.
Nilesh Agniohtri 14-Aug-13 5:09am    
Make Username column in your table as primary key so that insert query will not allow duplicate usernames in that column instead it will show error message 'record alredy exist' .
ridoy 14-Aug-13 6:04am    
incomplete question.
Member 10208820 15-Aug-13 22:23pm    
thanks all. it's working now.

Hi,
u can use constraints in table. Make a unique constraint on username. whenever u try to insert duplicate entry then database will show error. Handle that exception in code
 
Share this answer
 
Make the Username field Primary key in Table; So that when you add the existing user name, it will generate an exception, you can catch it and do the needful...

See this link to do it in SQL Server Management Studio

http://sqldud.blogspot.ae/2010/01/assign-primary-key-in-sql-server.html
 
Share this answer
 
v2

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