Predicates, Guids, and Strings, oh my!

Posts   
 
    
MikeG
User
Posts: 23
Joined: 17-Dec-2006
# Posted on: 17-Dec-2006 23:42:11   

Hi,

I am very new to the LLBLGen Universe, and am very excited to be making my first steps in learning how to use it well and proper. I have been up for a long while making baby steps forward, and I can now move data in and out of the database with SelfService code. It's a great start. And, I am a bit stuck now too and would like to ask for help, please.

I have two LLBLGenProDataSource components in an ASP.NET project. One of them is for a collection of User records. The other is for the detail of a User record. The Collection is bound to a DataGrid, and the Detail DataSource is bound to a DetailsView control.

I would like to select a row in the DataGrid, and based on the Guid value of the row, call up the Details for the DetailsView. The Guid is being used as the PrimaryKey for the UserEntity.

The DataGrid has a Button on each row that generates a CommandArgument which contains the Guid of the Row. This button works in moving the correct data to the method. However, the code I am using to update the Detail DataSource is crashing with this error:

Invalid cast from 'System.String' to 'System.Guid'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.

The code I am using to process this request is:

    protected void userCollectionDataGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ViewUserDetail") 
        {
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, e.CommandArgument));

            userDS.FilterToUse = filter;
            userDS.EntityCollection.FindMatches(filter);
        } 
    }

Can you please tell me what I am doing wrong?

I am using LLBLGen.Pro 2.0 with SQL Server 2005, and ASP.NET 2.0.

Thank you!

smile Mike

younghov
User
Posts: 8
Joined: 14-Nov-2006
# Posted on: 18-Dec-2006 01:02:22   

MikeG wrote:

Hi,

I am very new to the LLBLGen Universe, and am very excited to be making my first steps in learning how to use it well and proper. I have been up for a long while making baby steps forward, and I can now move data in and out of the database with SelfService code. It's a great start. And, I am a bit stuck now too and would like to ask for help, please.

I have two LLBLGenProDataSource components in an ASP.NET project. One of them is for a collection of User records. The other is for the detail of a User record. The Collection is bound to a DataGrid, and the Detail DataSource is bound to a DetailsView control.

I would like to select a row in the DataGrid, and based on the Guid value of the row, call up the Details for the DetailsView. The Guid is being used as the PrimaryKey for the UserEntity.

The DataGrid has a Button on each row that generates a CommandArgument which contains the Guid of the Row. This button works in moving the correct data to the method. However, the code I am using to update the Detail DataSource is crashing with this error:

Invalid cast from 'System.String' to 'System.Guid'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.

The code I am using to process this request is:

    protected void userCollectionDataGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ViewUserDetail") 
        {
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, e.CommandArgument));

            userDS.FilterToUse = filter;
            userDS.EntityCollection.FindMatches(filter);
        } 
    }

Can you please tell me what I am doing wrong?

I am using LLBLGen.Pro 2.0 with SQL Server 2005, and ASP.NET 2.0.

Thank you!

smile Mike

Thats probably because e.CommandArgument is a represented as string. Try the following...

filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, new Guid(e.CommandArgument)));

MikeG
User
Posts: 23
Joined: 17-Dec-2006
# Posted on: 18-Dec-2006 05:02:38   

Thats probably because e.CommandArgument is a represented as string. Try the following...

filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, new Guid(e.CommandArgument)));

Hi Younghov,

Thank you so much! That totally did it, with a slight little tweak. The ToString() wanted to also be added, and it works excellent!

The line is:

filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, new Guid(e.CommandArgument.ToString())));

Thank you much for your help!

Cheers, Mike

MikeG
User
Posts: 23
Joined: 17-Dec-2006
# Posted on: 18-Dec-2006 08:12:03   

Oh, and can you please tell me if this is the "right" way to do what I am wanting to do, in terms of commands used and sequence of execution:

    protected void userCollectionDataGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "ViewUserDetail") 
        {
            IPredicateExpression filter = new PredicateExpression();
            filter.Add(new FieldCompareValuePredicate(UserFields.Id, ComparisonOperator.Equal, new Guid(e.CommandArgument.ToString())));

            userDS.FilterToUse = filter;
            userDS.EntityCollection.FindMatches(filter);
        } 
    }

I have a feeling that I may not have it as good as it can be.

Thank you, Mike

smile

jbb avatar
jbb
User
Posts: 267
Joined: 29-Nov-2005
# Posted on: 18-Dec-2006 15:32:01   

Hello,

why do you add this line :


userDS.EntityCollection.FindMatches(filter);

This will send you all indices of all the entities of this collection which match the filter so you don't need it. You only need to set the filter in the llbl datasource.

MikeG
User
Posts: 23
Joined: 17-Dec-2006
# Posted on: 20-Dec-2006 07:34:25   

Hi JBB,

Thank you for sharing that with me! I have made the change to the code, and it works great without it.

I appreciate your help.

Cheers, Mike

simple_smile