Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.

Posted on 2009-04-20
2
Medium Priority
?
480 Views
Last Modified: 2012-05-06
Hi all,

I got this message after intentionally trying to enter a duplicate record in to my databse, as all the new records should be unique, I'd appreciate some help coding a unique record check within the code below and a message being displayed like "user account already exists, please try again" there is alread a message label available in the code below.

Ideally the check needs to happen whilst the original page is displayed, so the user does not have to go back to the previous page to enter all the details again.
Function AddUser(ByVal userName As String, ByVal userPass As String, ByVal busName As String, ByVal busType As String) As Integer
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=has a source"
            Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)
    
            Dim queryString As String = "INSERT INTO [Users] ([UserName], [UserPass], [BusName], [BusType]) VALUES (@UserName, @UserPass, @BusName, @BusType)"
            Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
            dbCommand.CommandText = queryString
            dbCommand.Connection = dbConnection
    
            Dim dbParam_userName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_userName.ParameterName = "@UserName"
            dbParam_userName.Value = userName
            dbParam_userName.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_userName)
            Dim dbParam_userPass As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_userPass.ParameterName = "@UserPass"
            dbParam_userPass.Value = userPass
            dbParam_userPass.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_userPass)
            Dim dbParam_busName As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_busName.ParameterName = "@BusName"
            dbParam_busName.Value = busName
            dbParam_busName.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_busName)
            Dim dbParam_busType As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
            dbParam_busType.ParameterName = "@BusType"
            dbParam_busType.Value = busType
            dbParam_busType.DbType = System.Data.DbType.String
            dbCommand.Parameters.Add(dbParam_busType)
    
            Dim rowsAffected As Integer = 0
        dbConnection.Open()
            Try
            rowsAffected = dbCommand.ExecuteNonQuery
            Finally
                dbConnection.Close
            End Try
    
            Return rowsAffected
        End Function
    
    
    Sub Button1_Click(sender As Object, e As EventArgs)
    If txtBusType.SelectedIndex = 0
            bustypeinvalid.Text = "Please Select a Business Type"
    Else If AddUser(txtUserName.Text, txtUserPassword.Text, txtBusName.Text, txtBusType.SelectedIndex) > 0
            Response.Redirect("signed.aspx")
       Else
         message.Text = "YOU ACCOUNT HAS NOT BEEN CREATED PLEASE TRY AGAIN, IF THE PROBLEM PERSISTS TRY LATER OR CALL SUPPORT"
    End If
    End Sub
    
    Sub Button2_Click(sender As Object, e As EventArgs)
    txtusername.Text = ""
    txtuserpassword.Text = ""
    txtconfirmpassword.Text = ""
    txtbusname.Text = ""
    txtbustype.SelectedIndex = 0
    End Sub

Open in new window

0
Comment
Question by:Zoooink
2 Comments
 
LVL 20

Accepted Solution

by:
informaniac earned 2000 total points
ID: 24183492
Try using

try catch in ur function and main page both.

In the main page in the catch section, Just display the error message to the user
0
 
LVL 3

Author Closing Comment

by:Zoooink
ID: 31572172
Didin't realise it was so easy, thanks for the help :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question