Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 28th, 2006, 06:38 PM
Registered User
 
Join Date: Jun 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default mysterious problem (data no inserting )

I have a little problem which i want to discuss with you.
I writed the following code which is incrementing the id automatically.
But this code working fine till we are inserting he 10th record i.e. emp10.
But when we insert the 11th record, i.e. emp11,it starts throwing the error message:

Voilation of primary key constraint 'PK_EmpRegistration'. Cannot insert duplicate key in object 'EmpRegistration'. The Statement has been terminated.Can you help me how this problem can be rectified.



CODE::::::::::::::::::::::::::



 Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim intMaxID As Integer
        Dim strID As String

        Dim objcommand As SqlCommand = New SqlCommand

        Dim maxIDcommand As SqlCommand = New SqlCommand _
            ("Select MAX(ID) as maxID from EmpRegistration " & _
             "Where ID like 'emp%'", objConnection)

        'Open the connection, execute the command
        objConnection.Open()

        Dim maxID As Object = maxIDcommand.ExecuteScalar()

        'If the MaxID column is null...
        If maxID Is DBNull.Value Then
            'Set a default value of 1...
            intMaxID = 1
        Else
            'Otherwise set the strID variable to the value in MaxID...
            strID = CType(maxID, String)
            'Get the integer part of the string...
            intMaxID = CType(strID.Remove(0, 3), Integer)
            'Increment the value...
            intMaxID = intMaxID + 1
        End If

        strID = "emp" & intMaxID.ToString

        objcommand.Connection = objConnection
        objcommand.CommandText = "Insert into EmpRegistration " & _
           "(ID, Name, Address, BirthDate, Email, Qualification, PhNo,Experience, " & _
            "Reference, JoinDate, Salary) Values (@ID, @Name, @Address, @BirthDate, @Email, " & _
            "@Qualification, @PhNo, @Experience, @Reference, @JoinDate, @Salary);"

        objcommand.Parameters.Add("@ID", strID)
        objcommand.Parameters.Add("@Name", txtEmpName.Text)
        objcommand.Parameters.Add("@Address", txtAddress.Text)
        objcommand.Parameters.Add("@BirthDate", txtDOB.Text)
        objcommand.Parameters.Add("@Email", txtEmail.Text)
        objcommand.Parameters.Add("@Qualification", txtQualification.Text )
        objcommand.Parameters.Add("@PhNo", txtPhNo.Text)
        objcommand.Parameters.Add("@Experience", cboEmpRegistrationExperience.SelectedIndex)
        objcommand.Parameters.Add("@Reference", txtReference.Text)
        objcommand.Parameters.Add("@JoinDate", txtJoinDate.Text)
        objcommand.Parameters.Add("@salary", txtSalary.Text)

        Try
            objcommand.ExecuteNonQuery()
        Catch SqlExceptionErr As SqlException
            MessageBox.Show(SqlExceptionErr.Message)
        End Try

        objConnection.Close()

        FillDataSetAndView()
        BindFields()

    End Sub

:::::::::::::::::::::::::::::::

 
Old June 29th, 2006, 08:48 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

can you just follow the code and check out the PK that you are inserting??

you are inserting the same PK more than one time... if ID is the PK.. why are you using string fields for the PK??

HTH

Gonzalo





Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in inserting data in gridview deb_kareng ASP.NET 2.0 Professional 0 August 22nd, 2007 02:07 AM
problem in inserting data monikanit ASP.NET 1.x and 2.0 Application Design 1 July 30th, 2007 02:48 AM
PROBLEM Inserting data into 2 tbls that r JOINED shyster1977 Classic ASP Databases 2 March 10th, 2005 10:40 AM
Problem inserting with data adapter cridley ADO.NET 1 February 22nd, 2005 05:02 PM
problem in inserting blob data to oracle selphyr JSP Basics 2 December 2nd, 2004 01:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.