FetchEntityUsingUniqueConstraint returns false the second time

Posts   
 
    
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 16-Dec-2010 21:18:33   

Hallo all,

I'm having an odd thing while using Authorization like sample with Adapter.

This is the logic in the app I created:

  1. The user login with the supplied username and password.
  2. The user logout. The application still running.
  3. The user tries to login again using the same username and password but this time without any success. (FetchEntityUsingUniqueConstraint returns false)

frmLogin.cs


bool success = LoginHelper.Login(parUserName, parUserPwd, RegistrySettings.MySqlConnection);

frmMain.cs - Logout Click Event


LoginHelper.Logout();

LoginHelper.cs


public static bool Login(string username, string password, string conn)
{
    var user = new StaffEntity { UserName = username };

    var prefetch = new PrefetchPath2((int)EntityType.StaffEntity);
    prefetch.Add(StaffEntity.PrefetchPathDepartmentCollectionViaStaffGroup);

    using (var adapter = new DataAccessAdapter(conn))
    {
        //if (adapter.FetchEntity(user, prefetch))
        if (adapter.FetchEntityUsingUniqueConstraint(user, user.ConstructFilterForUCUserName(), prefetch))
            // the User must exist and provide valid password and must be active
            if (!user.IsNew && user.Password == password && user.Active != false)
            {
                _currentUser = user;
                _connString = conn;
                _isConfigWizard = false;
                return true;
            }
    }

    return false;
}

PS I'm using latest version.

Regards, Dave

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 16-Dec-2010 21:29:30   

Can you post the generated SQL for each case - this may give us an idea what is going on.

Matt

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 16-Dec-2010 23:03:57   

Of course! wink

Btw, I'm using MySQL 5.1 with latest version of Devart dotConnect for MySQL

First run:


Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT `staff`.`Active`, `staff`.`Address`, `staff`.`Address2`, `staff`.`AddressNr`, `staff`.`BirthDate`, `staff`.`BranchID` AS `BranchId`, `staff`.`CityID` AS `CityId`, `staff`.`CountryID` AS `CountryId`, `staff`.`CURP` AS `Curp`, `staff`.`DistrictID` AS `DistrictId`, `staff`.`FName` AS `Fname`, `staff`.`HireDate`, `staff`.`LastUpdate`, `staff`.`LName` AS `Lname`, `staff`.`Mail`, `staff`.`Mobile`, `staff`.`ObservationID` AS `ObservationId`, `staff`.`Password`, `staff`.`Phone`, `staff`.`PictureID` AS `PictureId`, `staff`.`PostalCodeID` AS `PostalCodeId`, `staff`.`StaffID` AS `StaffId`, `staff`.`UserName` FROM `staff` WHERE ( `staff`.`UserName` = @p1)
    Parameter: @p1 : AnsiString. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "Dave".
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.
The thread '<No Name>' (0x15b8) has exited with code 0 (0x0).
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT DISTINCT `department`.`DepartmentID` AS `DepartmentId`, `department`.`LastUpdate`, `department`.`Name` FROM (( `staff` `LPA_S1`  INNER JOIN `staffgroup` `LPA_S2`  ON  `LPA_S1`.`StaffID`=`LPA_S2`.`StaffID`) INNER JOIN `department`  ON  `department`.`DepartmentID`=`LPA_S2`.`DepartmentID`) WHERE ( ( ( `LPA_S2`.`StaffID` = @p1)))
    Parameter: @p1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 2.
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT DISTINCT `LPA_S1`.`StaffID` AS `StaffId0`, `department`.`DepartmentID` AS `DepartmentId1` FROM (( `staff` `LPA_S1`  INNER JOIN `staffgroup` `LPA_S2`  ON  `LPA_S1`.`StaffID`=`LPA_S2`.`StaffID`) INNER JOIN `department`  ON  `department`.`DepartmentID`=`LPA_S2`.`DepartmentID`) WHERE ( ( ( ( `LPA_S2`.`StaffID` = @p1))))
    Parameter: @p1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 2.
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT `staff`.`Active`, `staff`.`Address`, `staff`.`Address2`, `staff`.`AddressNr`, `staff`.`BirthDate`, `staff`.`BranchID` AS `BranchId`, `staff`.`CityID` AS `CityId`, `staff`.`CountryID` AS `CountryId`, `staff`.`CURP` AS `Curp`, `staff`.`DistrictID` AS `DistrictId`, `staff`.`FName` AS `Fname`, `staff`.`HireDate`, `staff`.`LastUpdate`, `staff`.`LName` AS `Lname`, `staff`.`Mail`, `staff`.`Mobile`, `staff`.`ObservationID` AS `ObservationId`, `staff`.`Password`, `staff`.`Phone`, `staff`.`PictureID` AS `PictureId`, `staff`.`PostalCodeID` AS `PostalCodeId`, `staff`.`StaffID` AS `StaffId`, `staff`.`UserName` FROM `staff` WHERE ( ( `staff`.`StaffID` = @p1))
    Parameter: @p1 : Int32. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: 2.
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT `branch`.`Active`, `branch`.`Address`, `branch`.`Address2`, `branch`.`AddressNr`, `branch`.`BranchID` AS `BranchId`, `branch`.`CityID` AS `CityId`, `branch`.`CompanyID` AS `CompanyId`, `branch`.`CountryID` AS `CountryId`, `branch`.`CreatedDate`, `branch`.`DistrictID` AS `DistrictId`, `branch`.`Fax`, `branch`.`LastUpdate`, `branch`.`Mail`, `branch`.`Name`, `branch`.`Phone`, `branch`.`PostalCodeID` AS `PostalCodeId` FROM `branch` WHERE ( ( `branch`.`BranchID` = @p1))
    Parameter: @p1 : Byte. Length: 1. Precision: 0. Scale: 0. Direction: Input. Value: 1.
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.

Second run:


Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query: 
    Query: SELECT `staff`.`Active`, `staff`.`Address`, `staff`.`Address2`, `staff`.`AddressNr`, `staff`.`BirthDate`, `staff`.`BranchID` AS `BranchId`, `staff`.`CityID` AS `CityId`, `staff`.`CountryID` AS `CountryId`, `staff`.`CURP` AS `Curp`, `staff`.`DistrictID` AS `DistrictId`, `staff`.`FName` AS `Fname`, `staff`.`HireDate`, `staff`.`LastUpdate`, `staff`.`LName` AS `Lname`, `staff`.`Mail`, `staff`.`Mobile`, `staff`.`ObservationID` AS `ObservationId`, `staff`.`Password`, `staff`.`Phone`, `staff`.`PictureID` AS `PictureId`, `staff`.`PostalCodeID` AS `PostalCodeId`, `staff`.`StaffID` AS `StaffId`, `staff`.`UserName` FROM `staff` WHERE ( `staff`.`UserName` = @p1)
    Parameter: @p1 : AnsiString. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "Dave".
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.

The 2nd time it doesn't complete...

Walaa avatar
Walaa
Support Team
Posts: 14981
Joined: 21-Aug-2005
# Posted on: 17-Dec-2010 09:33:40   

What do you have inside LoginHelper.Logout();

What happens if a user attempt to login twice in a row without loging out, can you simulate that?

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 17-Dec-2010 20:08:58   

Walaa wrote:

What do you have inside LoginHelper.Logout();

What happens if a user attempt to login twice in a row without loging out, can you simulate that?

This is the code inside Logout():


public static void Logout()
{
    _currentUser = null;
}

The code is practically the same as the sample provided in the downloads section, I just made some modifications because I pass the connection string inside Login();

Removing LoginHelper.Logout(); in Logout click event worked without the described problem. I was able to login, logout with the same and different user. disappointed

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 18-Dec-2010 00:10:59   

iDave wrote:

Walaa wrote:

What do you have inside LoginHelper.Logout();

What happens if a user attempt to login twice in a row without loging out, can you simulate that?

This is the code inside Logout():


public static void Logout()
{
    _currentUser = null;
}

The code is practically the same as the sample provided in the downloads section, I just made some modifications because I pass the connection string inside Login();

What did you download from download section?

iDave wrote:

Removing LoginHelper.Logout(); in Logout click event worked without the described problem. I was able to login, logout with the same and different user. disappointed

Are you using ASP.NET or Winforms? Where are you storing _currentUser?

David Elizondo | LLBLGen Support Team
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 18-Dec-2010 01:46:41   

daelmo wrote:

iDave wrote:

Walaa wrote:

What do you have inside LoginHelper.Logout();

What happens if a user attempt to login twice in a row without loging out, can you simulate that?

This is the code inside Logout():


public static void Logout()
{
    _currentUser = null;
}

The code is practically the same as the sample provided in the downloads section, I just made some modifications because I pass the connection string inside Login();

What did you download from download section?

iDave wrote:

Removing LoginHelper.Logout(); in Logout click event worked without the described problem. I was able to login, logout with the same and different user. disappointed

Are you using ASP.NET or Winforms? Where are you storing _currentUser?

Hallo David,

I downloaded the "Authorization example for Winforms". Most of the code is similiar, the only changes made are mentioned above. I'm using WinForms. If you like I can share the LoginHelper.cs file.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Dec-2010 21:19:21   

Change the logout to this:

public static void Logout()
{
    _currentUser = new UserEntity();
}
David Elizondo | LLBLGen Support Team
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 20-Dec-2010 01:14:42   

daelmo wrote:

Change the logout to this:

public static void Logout()
{
    _currentUser = new UserEntity();
}

Sorry, but changing to the suggested code doesn't work... disappointed

Walaa avatar
Walaa
Support Team
Posts: 14981
Joined: 21-Aug-2005
# Posted on: 20-Dec-2010 10:24:42   

I've made a simple test and I couldn't reprodiuce it.

  • Using LLBLGen Pro v.3.0, and generating code for .NET 3.5, VS2008
  • Using Northwind database, I've set teh CompanyName column in the Customers table to be a unique Key.
  • Created a windows form applcation, with one button in a form. The following is the code in the button click event:
private void button1_Click(object sender, EventArgs e)
{
    var customer = new CustomerEntity();
    customer.CompanyName = "Island Trading";

    using (var adapter = new DataAccessAdapter())
    {
        var result = adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCCompanyName());
        MessageBox.Show(result.ToString());
    }
}

No matter how much times I hit the button, the MessageBox shows "True".

How should I change my simple test to reproduce the issue you are having? Or could you attach a simple repro example.

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 20-Dec-2010 18:36:19   

Walaa wrote:

I've made a simple test and I couldn't reprodiuce it.

  • Using LLBLGen Pro v.3.0, and generating code for .NET 3.5, VS2008
  • Using Northwind database, I've set teh CompanyName column in the Customers table to be a unique Key.
  • Created a windows form applcation, with one button in a form. The following is the code in the button click event:
private void button1_Click(object sender, EventArgs e)
{
    var customer = new CustomerEntity();
    customer.CompanyName = "Island Trading";

    using (var adapter = new DataAccessAdapter())
    {
        var result = adapter.FetchEntityUsingUniqueConstraint(customer, customer.ConstructFilterForUCCompanyName());
        MessageBox.Show(result.ToString());
    }
}

No matter how much times I hit the button, the MessageBox shows "True".

How should I change my simple test to reproduce the issue you are having? Or could you attach a simple repro example.

Thank you Walaa for taking your time to reproduce my issue. I'll prepare a sample and attach it here, just give me time.

iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 20-Dec-2010 23:14:46   

Quick Update:

I just created the sample project and its working fine... the strange thing is that I'm using the same code. disappointed

I'm going to keep looking at it... will post an update here when I have something.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 21-Dec-2010 02:15:14   

Ok David. I will close this thread for now. When you come back and post, it will be automatically re-opened.

David Elizondo | LLBLGen Support Team
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 08-Jul-2011 21:04:56   

Oke, I'm reopening this thread 'cause I'm now getting this exception when previosly I didn't get any message...

Version: LLBLGen Pro 3.1 (latest) VS: Visual Studio 2010 Target: Microsoft .NET Framework 4.0

Thread Exception, The type initializer for 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2' threw an exception.; 
   at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2..ctor(String name)
   at ER.DAL.EntityClasses.CommonEntityBase..ctor(String name) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.DAL\DatabaseGeneric\EntityClasses\CommonEntityBase.cs:line 45
   at ER.DAL.EntityClasses.StaffEntity..ctor() in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.DAL\DatabaseGeneric\EntityClasses\StaffEntity.cs:line 102
   at ER.Identity.LoginHelper.Login(String username, String password, String conn) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.LoginHelper\LoginHelper.cs:line 76
   at ER.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER\Form1.cs:line 18
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Jul-2011 23:45:07   

It appears that the problem is at the StaffEntity constructor. Please post the exact exception message (not just the stack trace) and post your piece of code that throws the exception.

David Elizondo | LLBLGen Support Team
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 20-Jul-2011 05:12:51   

This is all I get...

Exception Message:

The type initializer for 'SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2' threw an exception.

Stack Trace:

at SD.LLBLGen.Pro.ORMSupportClasses.EntityBase2..ctor(String name)
at ER.DAL.EntityClasses.CommonEntityBase..ctor(String name) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.DAL\DatabaseGeneric\EntityClasses\CommonEntityBase.cs:line 45
at ER.DAL.EntityClasses.StaffEntity..ctor() in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.DAL\DatabaseGeneric\EntityClasses\StaffEntity.cs:line 102
at ER.Identity.LoginHelper.Login(String username, String password, String conn) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER.LoginHelper\LoginHelper.cs:line 76
at ER.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\iDave\Documents\Visual Studio 2010\Projekts\ER\ER\Form1.cs:line 18
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

And the code...

Button click event:

bool success = LoginHelper.Login("user", "123456", "host=localhost;user id=test;password=dbatester;database=tempdata;port=3306");

Login:

public static bool Login(string username, string password, string conn)
{
    var user = new StaffEntity { UserName = username };

    var prefetch = new PrefetchPath2((int)EntityType.StaffEntity);
    prefetch.Add(StaffEntity.PrefetchPathDepartmentCollectionViaStaffGroup);

    using (var adapter = new DataAccessAdapter(conn))
    {
        if (adapter.FetchEntityUsingUniqueConstraint(user, user.ConstructFilterForUCUserName(), prefetch))
            if (!user.IsNew && user.Password == password && user.Active != false)
            {
                _currentUser = user;
                _connString = conn;
                return true;
            }
    }

    return false;
}

It crash in the following line:

if (adapter.FetchEntityUsingUniqueConstraint(user, user.ConstructFilterForUCUserName(), prefetch))
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jul-2011 07:42:28   

I really can't reproduce it with exact code (except I tested in Test project, no web project) :\ Please try the following:

  1. Regenerate the code from your LLBLGen project. I think it could be outdated.

  2. Open your StaffEntity.cs, you have to see a constructor like:

public StaffEntity():base("StaffEntity")
{
     InitClassEmpty(null, null);
}

Is it look like that?

  1. Debug your app to see if you can step into and discover the InnerException, maybe that would give us some clues. You also could download the ORMSupportLibraries source code and it to your project to debug deeper.

  2. If all that fails, please try to pack a small solution that reproduce the problem and send it to us. You can attach it here, or you could open a new HelpDesk thread (private) if you want.

David Elizondo | LLBLGen Support Team
iDave avatar
iDave
User
Posts: 67
Joined: 14-Jun-2010
# Posted on: 27-Jul-2011 20:58:00   

Thank you David, I'm going to try your suggestions and let you know...