0

I built a query to find a user by the username. When I run the program I get:

InvalidOperationException: Sequence contains more than one element

I checked the test database, and there's with only 4 users and no double names. Where can the exception come from? Here is the query:

public void setUser(String userName)
{
    AzaraUser = DatabaseConnection.DataContext.GetTable<AzaraUser>()
                .SingleOrDefault(a => a.ProgramUserName == userName || a.UserName == userName);
}

With the tips below I tried debugging and found that the method becomes an empty string. So in this case it's normal that I get several responses because the ProgramUserName is only in a few rows not null because it is for user who log in with an active directory account.

But why do I get this empty string? Could it be that the WebSecurity is also empty on that moment? At which point will it get the information about the current user?

Here is my log in method:

[HttpPost]
public ActionResult Login(FormCollection logInForm)
{  
    // try the default membership auth 
    if (Membership.ValidateUser(logInForm["name"], logInForm["password"]))
    {
        FormsAuthentication.SetAuthCookie(logInForm["name"], false);
        user.setUser(WebSecurity.CurrentUserName);
        string returnUrl = Request.QueryString["ReturnUrl"];

        if (returnUrl == null)
        {
            Response.Redirect("~/home/index");
        }
        else
        {
            Response.Redirect(returnUrl);
        }
    }
    else
    {
        ModelState.AddModelError("", "Login failed");
    } 

    // try to auth user via AD
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
    {
        if (pc.ValidateCredentials(logInForm["name"], logInForm["password"]))
        {
            FormsAuthentication.SetAuthCookie(logInForm["name"], false);
            user.setUser(WebSecurity.CurrentUserName);
            return RedirectToAction("Index", "Home");
        }
    }

    return View("LogOn");
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
PX Roth
  • 139
  • 2
  • 16
  • WebSecurity.CurrentUserName gives the string "" to setUser. This is searching then in the database. In the database there are 4 records at moment. Only one have a ProgramUserName, all the other records have null in this row. I fixed the problem now with a work around so the program is running but i like to know at which time WebSecurety get the username of the loged in user. – PX Roth Jun 21 '13 at 06:37

2 Answers2

1

This exception only occur if there are more than one records. Try to run the query in Sql Server management studio or whatever you have or try to debug the code and see what is value of userName. e.g

SELECT * FROM AzaraUser WHERE ProgramUserName = 'ABC' OR UserName  = 'ABC'

or try:

public List<AzaraUser> setUser(String userName)
{
 AzaraUser = DatabaseConnection.DataContext.GetTable<AzaraUser>().WHERE(a => a.ProgramUserName == userName || a.UserName == userName).ToList<AzaraUser>();
}
Altaf Sami
  • 846
  • 5
  • 10
  • 21
1

The error is pretty explicit. Try changing SingleOrDefault to a Where and add ToList to the end, then place a breakpoint on the next line so you can see the actual rows being returned.

AzaraUser = DatabaseConnection.DataContext.GetTable<AzaraUser>()
            .Where(a => a.ProgramUserName == userName || a.UserName == userName)
            .ToList();

Also, make doubly sure you're connecting to your test database and not some other database where records are duplicated.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
  • he says that there are no duplicate names. – Altaf Sami Jun 20 '13 at 12:02
  • I found out why i get more than one result and the problem occures a little bit earlier in the code. At moment i´m not sure why this happens so i complement my question with the informations. – PX Roth Jun 20 '13 at 12:10