Rafael Lenartowicz
3/27/2007 5:46:00 PM
I don't know the front end language you're using,
but if it's NULL that "bothers" the compiler -
replace first part of your select statement instead of :
"SELECT U.UserID, L.SessionID FROM ... "
use
"SELECT U.UserID, IsNull( L.SessionID, 0 ) FROM ... "
it will return 0 (not null) if no sessions found.
or even simpler - use INNER JOIN. If there is no session for your user id -
no rows will be returned at all.
r
"David Trasbo" <davidtrasbo@gmail.com> wrote in message
news:1174999348.510115.159460@y66g2000hsf.googlegroups.com...
> Roy Harvey <roy_har...@snet.net> wrote:
>> SELECT A.UserID,
>> B.SessionID
>> FROM Users as A
>> LEFT OUTER
>> JOIN LoginSessions as B
>> ON A.UserID = B.UserID
>> WHERE A.UserName = 'Marx'
>> AND A.UserPassword = 'swordfish'
>
> OK, I'm executing following code (via remoting):
>
> public bool LoggedIn(string userName, string userPassword)
> {
>
> SqlCommand findLoginSessionSqlCommand;
> SqlParameter userNameParameter;
> SqlParameter userPasswordParameter;
> SqlDataReader findLoginSessionSqlDataReader;
>
> try
> {
> runnerSqlConnection.Open();
> }
> catch
> {
> throw;
> }
>
> findLoginSessionSqlCommand = new SqlCommand("SELECT
> U.UserID, L.SessionID FROM Users as U LEFT OUTER JOIN LoginSessions as
> L ON U.UserID = L.UserID WHERE U.UserName = @UserName AND
> U.UserPassword = @UserPassword", runnerSqlConnection);
>
> userNameParameter = new SqlParameter("@UserName",
> userName);
> userPasswordParameter = new SqlParameter("@UserPassword",
> userPassword);
>
>
> findLoginSessionSqlCommand.Parameters.Add(userNameParameter);
>
> findLoginSessionSqlCommand.Parameters.Add(userPasswordParameter);
>
> userNameParameter.Value = userName;
> userPasswordParameter.Value = userPassword;
>
> findLoginSessionSqlDataReader =
> findLoginSessionSqlCommand.ExecuteReader(System.Data.CommandBehavior.SingleRow);
>
> if (findLoginSessionSqlDataReader[1] != null)
> <=<=<=<=<=<=<=<=<= LOOK HERE!
> {
> runnerSqlConnection.Close();
> return true;
> }
> else
> {
> runnerSqlConnection.Close();
> return false;
> }
> }
>
> On the line with findLoginSessionSqlDataReader[1] I am getting an
> exception: InvalidOperationException. Right now the second column
> [zero-based: 1] returns NULL because their is no sessions. But I don't
> understand that exception!?
>
> --
> David Trasbo.
>