Hi, can you please help me here. I am having this error, when I try to insert data. I have two tables with a foreign key relationship. I tried to debug the exception happens when it reaches
cmd.ExecuteNonQuery();. Please help me.
CREATE TABLE TB_User
(
UserId varchar(128) PRIMARY KEY NOT NULL,
FirstName varchar(50) NOT NULL,
LastName varchar (50) NOT NULL,
Email varchar (128) NOT NULL,
PhoneNumber varchar(15) NOT NULL,
UserType varchar(50) NOT NULL,
Password varchar (128) NOT NULL,
)
CREATE TABLE TB_Property
(
PropertyId varchar(128) PRIMARY KEY NOT NULL,
PropertyType varchar (128) NOT NULL,
Area varchar(50) NOT NULL,
Price decimal (18,0) NOT NULL,
UserId varchar(128) FOREIGN KEY REFERENCES TB_User(UserId) NOT NULL
)
I got the current user id from query string, then after that, I tried to store the Id in property table. while debugging, I tried to check the query string, the Id exists already in the user table.
if (Request.QueryString["UserId"].ToString() != null)
{
currentUserId = Request.QueryString["UserId"].ToString();
}
This is the insertion query.
SqlCommand cmd = new SqlCommand("INSERT INTO TB_Property(PropertyId, PropertyType, Area, Price,UserId) VALUES(NEWID(), @PropertyType, @Area,@Price, '"+ currentUserId +"')", conn);