Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have validate an item which does not allow duplicate value by using this code but i have problem on it

VB
If SQLstr <> Nothing Then
               MessageBox.Show("name is already used ")

           End If

this is my source code:

SQL
Try
           Dim curdate As String
           Dim recstatus As String



           curdate = Now.Date
           recstatus = "A"

           If txtuser.Text = "" Then
               MessageBox.Show("Blank Record is NOT accepted")
               Exit Sub
           End If

           If txtpass.Text = "" Then
               MessageBox.Show("Blank Record is NOT accepted")
               Exit Sub
           End If
           If txtcpass.Text = "" Then
               MessageBox.Show("Blank Record is NOT accepted")
               Exit Sub
           End If

           If txtpass.Text.Trim <> txtcpass.Text.Trim Then
               MessageBox.Show("Both Password and Confirm Password Not Match")
               Exit Sub
           End If

           If (Comlevel.SelectedIndex = -1) Or Comlevel.Text = "" Then
               MessageBox.Show("Please select Level")
               Exit Sub
           End If



           Mycn = New SqlConnection("Data Source=IMAN-PC;Initial Catalog=control car;Integrated Security=True")
           Mycn.Open()
           SQLstr = "insert into login values('" & recstatus & "','" & txtuser.Text & "','" & txtpass.Text & "','" & Comlevel.Text & "','" & usrID & "','" & curdate & "','" & usrID & "', '" & curdate & "')"
           command = New SqlCommand(SQLstr, Mycn)
           icount = command.ExecuteNonQuery

           If SQLstr <> Nothing Then
               MessageBox.Show("name is here")

           End If

           If icount = 1 Then
               MessageBox.Show("Record Added Successfully", "Add Record", MessageBoxButtons.OK, MessageBoxIcon.Information)
           Else
               MessageBox.Show("No Record Inserted", "Add Record", MessageBoxButtons.OK, MessageBoxIcon.Information)

           End If




           Mycn.Close()

       Catch ex As Exception


           MessageBox.Show(ex.Message & "Could Not Insert Record")
           'Mycn.Close()
Posted
Comments
codeBegin 4-May-12 7:59am    
what is your problem
dr.dream 4-May-12 8:04am    
i think it is on for loop statment
it show the message(name is already used) then
it show(record add successfuly)

You may use the SQL clause EXIST to avoid inserting duplicate rows, see, for instance, the first question in the FAQ here[^].
 
Share this answer
 
Comments
Maciej Los 4-May-12 12:29pm    
Nice link with examples. My 5
CPallini 4-May-12 16:11pm    
Thank you.
You Have to Check that the User ID is Already Exist Or Not in DataBase.Refer the following Code :
VB
Dim _Result As Object
Dim _ObjCmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand
       _ObjCmd.CommandText = "Select * From Login Where UserID ='" & txtUser.Text & "'"
       _ObjCmd.Connection = _SQLConnection
       Try
           _SQLConnection.Open()
           _Result = _ObjCmd.ExecuteReader
           _SQLConnection.Close()
       Catch ex As Exception

       End Try
       If _Result Is DBNull.Value Then
           'Your Insert Code
       Else
           MessageBox.Show("User Id Already Exist.")
       End If

I hope it will help you.:)
 
Share this answer
 
Comments
Maciej Los 4-May-12 12:32pm    
Good answer. My 5
Manoj K Bhoir 4-May-12 23:57pm    
Thanks losmac!
Your variable SQLstr is never going to equal nothing because you set the variable to your insert SQL three lines before that. So your if statement...
VB
If SQLstr <> Nothing Then
    MessageBox.Show("name is here")
End If

will ALWAYS be hit because SQLstr will ALWAYS have the text you set in it three lines before.

It's not the correct value to check to see if you already have a value in your database. I agree with Manoj K Bhoir and CPallini that you need to perform the check separately and by other means.
 
Share this answer
 
Comments
Maciej Los 4-May-12 12:31pm    
Holy true! My 5
Manoj K Bhoir 4-May-12 23:56pm    
My 5!

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