Pippa Daly

Pippa Daly

  • NA
  • 28
  • 850

Problem with Primary key in database when using web forms

Feb 10 2018 1:00 PM
I'm getting an issue when I try to input data into a web page. However the issue is with my primary/foreign key in the database. Any help would be much appreciated.
 
Error message:
Error:System.Data.SqlClient.SqlException (0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "UserIDfk". The conflict occurred in database "\\MAC\HOME\DOCUMENTS\COLLEGE\4TH YEAR\FYP\ITERATION 1\FYP-NEW\FYP-PARKWAY\FYP-PARKWAY\APP_DATA\USERDATA.MDF", table "dbo.Registration", column 'UserID'. The statement has been terminated. at System.Data.SqlClient.SqlConnection.onerror(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.onerror(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(Runbehavior runbehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, Runbehavior runbehavior, String resetOptionsString, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(Commandbehavior cmdbehavior, Runbehavior runbehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(Commandbehavior cmdbehavior, Runbehavior runbehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at FYP_ParkWay.ListSpace.Button1_Click(Object sender, EventArgs e) in \\Mac\Home\Documents\College\4th Year\FYP\Iteration 1\FYP-NEW\FYP-ParkWay\FYP-ParkWay\ListSpace.aspx.cs:line 30 ClientConnectionId:dbdeb098-62a6-45bf-960b-374be8a4e13f Error Number:547,State: 0,Class:16
 
My primary key is:
  1. CREATE TABLE [dbo].[Registration] (  
  2. [UserID] INT IDENTITY (1, 1) NOT NULL,  
  3. [NameNCHAR (20) NULL,  
  4. [Surname] NCHAR (20) NULL,  
  5. [Email] NCHAR (30) NULL,  
  6. [Address] NCHAR (50) NULL,  
  7. [Phone] NVARCHAR (50) NULL,  
  8. [PasswordNCHAR (30) NULL,  
  9. CONSTRAINT [pk_UserID] PRIMARY KEY CLUSTERED ([UserID] ASC)  
  10. )  
The insert statement in which its referring is: 
  1. SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);  
  2. conn.Open();  
  3.   
  4. String insertQuery = "insert into ListSpace (Area,Date,StartTime,EndTime,Picture) values(@area, @date, @startTime, @endTime, @picture)";  
  5. SqlCommand com = new SqlCommand(insertQuery, conn);  
  6. com.Parameters.AddWithValue("@area", ddlArea.Text);  
  7. com.Parameters.AddWithValue("@date", cdrDate.SelectedDate);  
  8. com.Parameters.AddWithValue("@startTime", ddlStart.Text);  
  9. com.Parameters.AddWithValue("@endTime", ddlEnd.Text);  
  10. com.Parameters.AddWithValue("@picture", updPicture.FileBytes);  
  11.   
  12. com.ExecuteNonQuery();  

Answers (1)