create table [User]
(
userId int IDENTITY(1,1) primary key ,
UserName nvarchar(30) unique,
gender nvarchar(2),
check( gender in ('M','F')),
email nvarchar(40),
DOB date
)
create table User_Auth
(
uName nvarchar(30),
foreign key (uName) references [User](UserName),
pass nvarchar(30)
)
ALTER PROCEDURE [dbo].[CHECKREGISTERCREDENTIALSFORUSER]
@USRNAME NVARCHAR(30),
@DATE_OF_BIRTH DATE,
@EMAIL NVARCHAR(50),
@PASSWORD NVARCHAR(50),
@GENDER NVARCHAR(5),
@enterflag int output
AS
BEGIN
SET @enterflag = 0;
IF NOT EXISTS (SELECT UserName FROM [User] u WHERE u.UserName = @USRNAME)
BEGIN
IF (@DATE_OF_BIRTH <= GETDATE())
BEGIN
INSERT INTO [User] (UserName, gender, email, DOB)
VALUES (@USRNAME, @GENDER, @EMAIL, @DATE_OF_BIRTH)
INSERT INTO [User_Auth] (uName, pass)
VALUES (@USRNAME, @PASSWORD)
SET @enterflag = 1;
END
ELSE
BEGIN
PRINT 'invalid birth date entered';
END
END
ELSE
BEGIN
PRINT 'username is taken';
END
END
public static int Signup(string uname, string password, DateTime DOB, string email, string genders)
{
SqlConnection con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd;
int result = 0;
try
{
if (con != null && con.State == ConnectionState.Open)
{
cmd = new SqlCommand("CHECKREGISTERCREDENTIALSFORUSER", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add("@USRNAME", SqlDbType.NVarChar, 30).Value = uname;
cmd.Parameters.Add("@PASSWORD", SqlDbType.NVarChar, 50).Value = password;
cmd.Parameters.Add("@DATE_OF_BIRTH", SqlDbType.Date).Value = DOB;
cmd.Parameters.Add("@GENDER", SqlDbType.NVarChar, 5).Value = email;
cmd.Parameters.Add("@EMAIL", SqlDbType.NVarChar, 50).Value = genders;
cmd.Parameters.Add("@enterflag", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
result = Convert.ToInt32(cmd.Parameters["@enterflag"].Value);
}
}
catch (SqlException ex)
{
Console.WriteLine("SQL Error" + ex.Message.ToString());
result = -1; //-1 will be interpreted as "error while connecting with the database."
}
finally
{
con.Close();
}
return result;
}
Here is the C# code - I can't figure out what is wrong - I get an error. There is not out of bound input in procedure parameters. Size of parameters in stored procedure is same as written in code. It is supposed to return 1 if sign up is successful. Moreover in my procedure I am adding into parent table and then to child table, still I get an error of
Insert statement conflict with foreign key constraint
The values for email
and gender
are mixed around when you set the parameters, so you're actually setting the email
value to the gender
in the database and vice versa.
cmd.Parameters.Add("@GENDER", SqlDbType.NVarChar, 5).Value = email;
cmd.Parameters.Add("@EMAIL", SqlDbType.NVarChar, 50).Value = genders;
should be
cmd.Parameters.Add("@GENDER", SqlDbType.NVarChar, 5).Value = genders;
cmd.Parameters.Add("@EMAIL", SqlDbType.NVarChar, 50).Value = email;
Additionally, in the User
table gender
is defined with length 2, but in the stored procedure it is declared length 5. Yet, you use check (gender in ('M', 'F'))
which is a single char.
If you're only accepting a single character for gender
, you could update both the User
table definition and stored proc to accept a single char for gender
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.