tags:

views:

90

answers:

2

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
+1  A: 

This bit of code looks dubious.

           con.Open();
           cmd.ExecuteNonQuery();
           Convert.ToInt32(cmd.Parameters["ret"].Value);

          int s = cmd.ExecuteNonQuery();
            cmd.Dispose();
           return s;

You are calling ExecuteNonQuery twice. and converting cmd.Parameters["ret"].Value to an int but not assigning the result to anything

Can you post the stored procedure code so we can see the intention?

Edit

I've marked my answer CW as I don't have the time or inclination to address all the issues but aside from the points above and the error you are getting about 'Entities.Entities'

That stored procedure looks doomed to failure as well

    SELECT @Ap = @Pass
    FROM   InventoryManager.dbo.Users
    WHERE  UserName = @User

If a correct UserName is passed in it will set the variable @Ap to the value of the variable @Pass that you originally passed in (without checking the value stored in the table at all) and return 1. I doubt this is the intention?

I should probably mention that it is decidedly non best practice to store passwords in clear text.

Martin Smith
ALTER PROCEDURE [dbo].[sp_SelectUser] -- Add the parameters for the stored procedure here @User varchar(50), @Pass varchar(50)ASDECLARE @Ap as varchar(50) -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. -- Insert statements for procedure hereSELECT @Ap = @Pass from InventoryManager.dbo.Users where UserName = @User if @Ap is null return -1 else if @Ap=@Pass return 1 else return -2
Crimsonland
What shall id do?
Crimsonland
+2  A: 

The error you are receiving is because SearchEntry() needs a UsersEntity as a parameter but you are calling it with two strings.

Try replacing:

int r = ****(int)aBl.SearchEntry(uEt.UserName,uEt.Password);****

with:

int r = (int)aBl.SearchEntry(uEt);

That being said, there may be more errors in your code.

Xavier Poinas
I just replaced that but it always go on message box: Incorrect User Id
Crimsonland
In SearchEntry(), try return (int)p1.Value; instead of return s; But again, there may be more errors.
Xavier Poinas