violating the primary key in the database

Howlleo

Member
Joined
Jan 14, 2008
Messages
13
Programming Experience
Beginner
Perfectly.
Thanks.
I'm running out of time, so I'm posting this question too even though I'm not sure you can help me with it.

I am now receiving an error about violating the primary key in the database. The primary key is a combination of SectionID, StudentID, Semester, and Year, to ensure that nobody can enroll in two classes at the same time.

I am not trying to enroll anyone in the same Section of the same semester.
I've tried INSERT through SQL Management Server Express Studio (without parameters, obviously, but similar values) and get no error. So I am tentatively assuming the problem is in the website code again.

Appreciate any input, even just ideas. Need this uploaded before midnight.

VB.NET:
Sub WriteClasses(ByVal CreditsArray)
        Dim i As Integer
        Dim EnrollDb As SqlConnection
        Dim cmdEnroll As SqlCommand
        EnrollDb = New SqlConnection("Server=LONNA\SQLEXPRESS;Integrated Security=True;database=LGordonTouroReg")
        cmdEnroll = New SqlCommand("INSERT INTO Enrollment (SectionID, Semester, Year, ClassID, StudentID) VALUES (@SectionID, 'Fall', '2007', @ClassID, @StudentID)", EnrollDb)
        cmdEnroll.Parameters.AddWithValue("@SectionID", CreditsArray(i, 2))
        cmdEnroll.Parameters.AddWithValue("@ClassID", CreditsArray(i, 0))
        cmdEnroll.Parameters.AddWithValue("@studentID", Profile.StudentID)
        EnrollDb.Open()
        For i = 0 To 12
            If Not CreditsArray(i, 0) = "" Then
                cmdEnroll.ExecuteNonQuery()
                Response.Write(CreditsArray(i, 0) & " has been added to your schedule.<br/>")
            End If
        Next i
        EnrollDb.Close()
    End Sub


ERROR MESSAGE:
Server Error in '/LGordonTouroReg' Application.
________________________________________
Violation of PRIMARY KEY constraint 'aaaaaEnrollment_PK'. Cannot insert duplicate key in object 'dbo.Enrollment'.
The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'aaaaaEnrollment_PK'. Cannot insert duplicate key in object 'dbo.Enrollment'.
The statement has been terminated.

Source Error:

Line 190: For i = 0 To 12
Line 191: If Not CreditsArray(i, 0) = "" Then
Line 192: cmdEnroll.ExecuteNonQuery()
Line 193: Response.Write(CreditsArray(i, 0) & " has been added to your schedule.<br/>")
Line 194: End If

Source File: C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx Line: 192

Stack Trace:

[SqlException (0x80131904): Violation of PRIMARY KEY constraint 'aaaaaEnrollment_PK'. Cannot insert duplicate key in object 'dbo.Enrollment'.
The statement has been terminated.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +177
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +68
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +199
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2411
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +147
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1038
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +314
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +413
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +115
ASP.registrationpage_aspx.WriteClasses(Object CreditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:192
ASP.registrationpage_aspx.NoDouble(Object CreditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:176
ASP.registrationpage_aspx.CheckPrereq(Object creditsArray) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:153
ASP.registrationpage_aspx.RegisterButton_Click(Object sender, EventArgs e) in C:\Users\Lonna\Documents\Visual Studio 2005\WebSites\LGordonTouroReg\RegistrationPage.aspx:25
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +97
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921

________________________________________
Version Information: Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.833
 
Hi -

Thanks. What I ended up doing was removing the primary key constraints in the database. Not a real world solution, but it works for academic deadlines. I'll give your idea a spin, though.

Thanks to you all for your help.
 
Back
Top