TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ravi Rai
NA
14
2.7k
The INSERT statement conflicted with the FOREIGN KEY??
Apr 26 2015 11:13 AM
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();
Reply
Answers (
14
)
how to use bar graph and pi graph in aspx page and to handle
Sql Queries and UserId (Need Skype Help Please)