aditya immadi

aditya immadi

  • NA
  • 215
  • 23.8k

confusion in primary key and foriegn key values insertion

May 19 2016 2:22 AM
Hai all i wrote this lines for creating password after registration page here id in Tb_User is primarykey and UserIDFromRegistration column is foriegnkey in tb_login
 
 
my contoller is
 
public ActionResult CreatePassword(Tbl_LoginCredentials user)
{
int userid = 0;
string name = null, Email = null;
name = Session["name"].ToString();
Email = Session["Email"].ToString();
int i = v.CreatePassword( user.Password, name, Email);
return view();
 
public int CreatePassword( string password, string name, string Email)
{
int i = 0;
SqlConnection con = new SqlConnection(s);
SqlCommand cmd = new SqlCommand("Sp_CreatePassword",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar)).Value = password;
cmd.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar)).Value = name;
cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar)).Value = Email;
con.Open();
i=cmd.ExecuteNonQuery();
con.Close();
return i;
 
 
i bring email and name from sessions and in case of id i think primarykey relation may work but it says
Cannot insert the value NULL into column 'UserIDFromRegistration', table 'JT.dbo.Tbl_LoginCredentials'; column does not allow nulls. INSERT fails.
 
and my stored proc is
 
ALTER Proc [dbo].[Sp_CreatePassword]
(
@Password nvarchar(50),
@UserName nvarchar(50),
@Email nvarchar(50)
)
as
begin
 
 
 
insert into Tbl_LoginCredentials (Password,UserName,Email) values (@Password ,@UserName ,@Email)
end
 
 
please help me...i struck in this for 2 days
 
TIA 
 
 
 

Answers (7)