Jorge
Jorge

Reputation: 18257

the sequence does not have elements executing linq query

Having this model class

public class Usuario
{
    #region Atributos
    private int _intID = 0;
    private Perfil _Perfil_FK = null;
    private String _strNombre = "";
    private String _strPassword = "";
    #endregion

    #region Propiedades
    public int ID
    {
        get { return _intID; }
        set { _intID = value; }
    }
    public virtual Perfil Perfil_FK
    {
        get { return _Perfil_FK; }
        set { _Perfil_FK = value; }
    }
    public int PerfilID { get; set; }
    public String Nombre
    {
        get { return _strNombre; }
        set { _strNombre = value; }
    }
    public String Password
    {
        get { return _strPassword; }
        set { _strPassword = value; }
    }
    #endregion
}

I'm trying to use this query, the data base table have data, so i can't see the problem?

 Usuario user = (from U in _db.Usuario
                            where ((U.Nombre == model.UserName) && (U.Password == encripPassword))
                            select U).First();

If you need more info about the data base let me know to update my question

Upvotes: 0

Views: 583

Answers (3)

neeKo
neeKo

Reputation: 4280

Check if your class has the proper attributes that tell Linq to SQL how to match it against a database table.

Your class should have

[Table(Name="Nombres")]

attribute, and properties should have

[Column]

attributes, some should also be primary keys etc.

If you don't already have this, I suggest you generate the class using the Entity Class Generation tool: http://msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic32

Upvotes: 0

p.campbell
p.campbell

Reputation: 100657

  • Does the table actually have a row with that name and password?
  • When you run the generated SQL statement in SQL Management Studio, do you get a result?

Suggest checking your two values in UserName and encripPassword for valid values.

Usuario user = _db.Usuario
                  .FirstOrDefault(x=>x.Nombre == model.UserName
                                 &&  x.Password == encripPassword);

string sql = (user as ObjectQuery).ToTraceString(); //the SQL query generated.

if(user==null)
{
  //doesn't exist.
}

Upvotes: 3

Daryl
Daryl

Reputation: 18895

Obviously you're not pointing to the right database, or your don't have a user name with that password (I'm guessing the encrypted password doesn't match what's in the DB. Try using FirstOrDefault(), then you can check for Null if the password is wrong...

Upvotes: 1

Related Questions