Keep getting the error: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Threads_Users". table "dbo.Users", column 'uid'.
Users table:
CREATE TABLE [dbo].[Users] (
[uid] INT IDENTITY (1, 1) NOT NULL,
[username] VARCHAR (14) NOT NULL,
[firstName] VARCHAR (20) NULL,
[lastName] VARCHAR (20) NULL,
[gender] VARCHAR (6) NULL,
[picture] VARCHAR (MAX) NULL,
CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED ([uid] ASC)
);
Threads table:
CREATE TABLE [dbo].[Threads] (
[ThreadId] INT IDENTITY (1, 1) NOT NULL,
[TopicId] INT NULL,
[Title] VARCHAR (50) NULL,
[Content] NTEXT NULL,
[PostedBy] VARCHAR (30) NULL,
[PostedDate] DATETIME NULL,
[uid] INT NULL,
CONSTRAINT [PK_Threads] PRIMARY KEY CLUSTERED ([ThreadId] ASC),
CONSTRAINT [FK_Threads_Users] FOREIGN KEY ([uid]) REFERENCES [dbo].[Users] ([uid]),
CONSTRAINT [FK_Threads_Threads] FOREIGN KEY ([TopicId]) REFERENCES [dbo].[Topics] ([TopicId])
);
SqlConnection conn;
SqlCommand comm;
conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("~\\App_Data\\ForumDB.mdf") + ";Integrated Security=True;User Instance=True");
conn.Open();
comm = new SqlCommand("INSERT INTO Threads values(" + Convert.ToInt32(Session["tid"].ToString()) + ",'" + Subject.Text + "','" + Post.Text + "','" + Session["uname"].ToString() + "','" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'," + Convert.ToInt32(Session["uid"]) + ")", conn);
int rows = comm.ExecuteNonQuery();
if (rows > 0)
{
Response.Redirect("threads.aspx?TopicId=" + Session["tid"].ToString());
}
conn.Close();