This is a migrated thread and some comments may be shown as answers.

Invalid cast from 'System.String' to 'System.Guid'

4 Answers 834 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jonathan
Top achievements
Rank 1
Jonathan asked on 06 Sep 2011, 12:06 PM
Hi, I'm sure there is a really simple answer to this, but...

I have a RadListBox bound to an OpenAccess datasource. The datasource is filtered on a querystring parameter, which is the string being passed to the parent form but the objects being retrieved use a GUID as the primary key and I get the error:

Invalid cast from 'System.String' to 'System.Guid'

Here is the definition for the datasource: 
<telerik:OpenAccessDataSource ID="OpenAccessDataSource2" runat="server" EnableDelete="False"
    EnableInsert="False" EnableUpdate="False" ObjectContextProvider="WorkProgramme.data.WPEntitiesModel, WorkProgramme.data"
    TypeName="WorkProgramme.data.ProjectDeliverable" Where="WpID == @WpID" 
    OrderBy="ProjectDeliverableDescription">
    <WhereParameters>
        <asp:QueryStringParameter Name="WpID" QueryStringField="projectID" />
    </WhereParameters>
</telerik:OpenAccessDataSource>

Here is a portion of the stack trace:

[InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.]
   System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) +9489160
   System.String.System.IConvertible.ToType(Type type, IFormatProvider provider) +8
   System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +9524264
   System.Convert.ChangeType(Object value, Type conversionType) +32
   Telerik.OpenAccess.RT.QueryImpl.Execute(Object[] args) +450
   Telerik.OpenAccess.RT.DataSource.OpenAccessDataSourceView.ExecuteOqlQuery(IQuery oqlQuery, Object[] queryArguments, Int32 startRowIndex, Int32 maximumRows) +136
   Telerik.OpenAccess.RT.DataSource.OpenAccessDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments) +1386
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +21
   Telerik.Web.UI.RadListBox.OnDataBinding(EventArgs e) +105
   Telerik.Web.UI.RadListBox.PerformSelect() +37

Any help would be appreciated.

Thanks,
Jonathan

4 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 08 Sep 2011, 05:03 PM
Hi Jonathan Simmons,

 Could you try adding the Type="System.Guid" and DbType="Guid" attributes to the parameter declaration? When the type of the parameter is not declared, the parameter is handled as a string one. I believe adding these two attributes should do the trick.

<asp:QueryStringParameter Name="WpID" QueryStringField="projectID" Type="System.Guid" DbType="Guid" />

Regards,
Zoran
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

0
Jonathan
Top achievements
Rank 1
answered on 09 Sep 2011, 11:20 AM
Hello Zoran,

This does not work as System.Guid does not exist in the list of options for Type and the page won't compile.

Cannot create an object of type 'System.TypeCode' from its string representation 'System.Guid' for the 'Type' property. 

I have tried all the various combinations of Object, String and Guid but still cannot get this to work.

Regards,
Jonathan
0
Jonathan
Top achievements
Rank 1
answered on 09 Sep 2011, 11:32 AM
Zoran,

With a little more fiddling I have managed to get this to work by:
- setting 'AutoGenerateWhereClause=True'
- setting 'DbType="Guid"'

Thanks,
Jonathan
0
Zoran
Telerik team
answered on 13 Sep 2011, 12:31 PM
Hi Jonathan Simmons,

 I am glad to know that you have managed to get over this issue. Please, let me apologize for the glitch from my side, DbType is the only property that can be specified for Guid parameters, Type is valid only for certain types of parameters and you are right, Guid is not part of those. 

Kind regards,
Zoran
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's SQL Server Community Awards. We are competing in TWO categories and every vote counts! VOTE for Telerik NOW >>

Tags
General Discussions
Asked by
Jonathan
Top achievements
Rank 1
Answers by
Zoran
Telerik team
Jonathan
Top achievements
Rank 1
Share this question
or