Hello i am just Studying 3-tier Application. I am now having a problem with this simple login form.
I always come up with this Error:
Entities.Entities is a type but is not
like used like a variable.
Always Shows: USer ID is incorrect.
public int SearchEntry(UsersEntity UserEntity)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = connStr;
try
{
SqlCommand cmd = new SqlCommand("sp_SelectUser", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@User", SqlDbType.VarChar, 50).Value = UserEntity.UserName;
cmd.Parameters.Add("@Pass", SqlDbType.VarChar, 50).Value = UserEntity.Password ;
SqlParameter p1 = new SqlParameter("ret", SqlDbType.Int);
p1.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(p1);
con.Open();
cmd.ExecuteNonQuery();
Convert.ToInt32(cmd.Parameters["ret"].Value);
int s = cmd.ExecuteNonQuery();
cmd.Dispose();
return s;
}
catch (SqlException)
{
throw;
}
finally
{
con.Close();
con.Dispose();
}
------------------------------------------------------------------------------------------
public class BLL
{
public int SearchEntry(UsersEntity UserEnity)
{
DAL pDAL = new DAL();
int i = pDAL.SearchEntry(UserEnity);
return i;
}
}
-------------------------------------------------------------------------------------------
PRESENTATION LAYER:
private void btnLogin_Click(object sender, EventArgs e)
{
BLL aBl = new BLL();
UsersEntity uEt = new UsersEntity();
uEt.UserName = txtUser.Text;
uEt.Password = txtPass.Text;
int r = ****(int)aBl.SearchEntry(uEt.UserName,uEt.Password);****
if (r == -1)
{
MessageBox.Show("Incorrect User Id");
}
else if (r == -2)
{
MessageBox.Show("Incorrect Password");
}
else if (r == 1)
{
MessageBox.Show("Welcome");
}
else
{
MessageBox.Show("Incorrect User Id / Password ");
}
}
Stored Procedure Code
ALTER PROCEDURE [dbo].[sp_SelectUser]
-- Add the parameters for the stored procedure here
@User VARCHAR(50),
@Pass VARCHAR(50)
AS
DECLARE @Ap AS VARCHAR(50)
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
-- Insert statements for procedure here
SELECT @Ap = @Pass
FROM InventoryManager.dbo.Users
WHERE UserName = @User
IF @Ap IS NULL
RETURN -1
ELSE
IF @Ap=@Pass
RETURN 1
ELSE
RETURN -2
The error you are receiving is because SearchEntry() needs a UsersEntity as a parameter but you are calling it with two strings.
Try replacing:
with:
That being said, there may be more errors in your code.