Skip to content Skip to sidebar Skip to footer

Help With Sql/linq Debugging

I'm having trouble with the following statement, which is returning the error 'Sequence contains no elements': var vUser = (from u in this.dcLAUNCHOnline.aspnet_Users

Solution 1:

.Single() always fails if the collection it is called upon is empty or contains more than one element. SQL Server does not return any rows, which must be the case here.

You could either use .FirstOrDefault() or .SingleOrDefault() and check the return value against null, depending on whether you expect a single element to be returned by your query.

E.g. you have a unique constraint on the row "UserName" you filter upon, you should use .SingleOrDefault(). If null is returned, no row has been found. Multiple rows wil never be returned.

Solution 2:

The InvalidOperationException that you are getting is thrown only when the query yielded no results.

The only thing that comes to my mind, (since you say that you are sure that the row exists on the database), is that you are maybe connecting to other database.

Check your DataContext's connection string and make sure you are querying the same database as in Management Studio.

Edit: BTW, you are querying directly the SqlMembershipProvideraspnet_Users table, to find users by UserName, you might want to give a look to the Membership.FindUsersByName method.

Post a Comment for "Help With Sql/linq Debugging"