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();
Vincent Maverick DuranoPosted Apr 26, 2015, 1:42 PM
Everyone is noob when learning something new so don't feel bad because I was once a noob too :) That's why you need to understand how stuff works and learn from it one step at a time
Ravi RaiPosted Apr 26, 2015, 1:37 PM
Ravi RaiPosted Apr 26, 2015, 1:23 PM
Vincent Maverick DuranoPosted Apr 26, 2015, 1:21 PM
Before referencing a Session value make sure that you check for null first Your Session may/will return string but you should do a null check on the Session object itself like so:
if(Session["KeyName"] != null){
//your code here to access the Session value
}
Abhineet SrivastavaPosted Apr 26, 2015, 1:18 PM
Ravi RaiPosted Apr 26, 2015, 1:17 PM
Abhineet SrivastavaPosted Apr 26, 2015, 1:16 PM
Ravi RaiPosted Apr 26, 2015, 1:14 PM
Abhineet SrivastavaPosted Apr 26, 2015, 1:11 PM
Ravi RaiPosted Apr 26, 2015, 1:09 PM
in my SQL statement i have:
In my debugger it says:
in my page load i have:
Vincent Maverick DuranoPosted Apr 26, 2015, 1:04 PM
The error message says you are trying to insert data that has a foreign key value that doesn't exist in the parent table. For example, you might have Users with Id values of 1,2,3,4 and 5, but you are trying to add a Thread with an Users of 6 (or, more likely, 0). You should use a debugger to double check the foreign key value.
Adding to that, I will not recommend you to append the TextBox values directly into your SQL statements because that would leads you to SQL Injection , use Parameterized Query instead for security and more cleaner codes.
Abhineet SrivastavaPosted Apr 26, 2015, 1:01 PM
First Check That, Is that Uid value exist in User table or not... If Not, Then you need to Insert That Uid value to User table first.
Ravi RaiPosted Apr 26, 2015, 11:53 AM
Manoj BhoirPosted Apr 26, 2015, 11:51 AM
I don't think there is any thing wrong in your code.
Please check that User Id i.e. uid you are inserting into the dbo.Threads table must exist in your dbo.Users table i.e. Primary Key Table.
For more details about Primary Key and Foreign Key see this link
https://msdn.microsoft.com/en-us/library/ms179610.aspx