GUID In Where Clause RRS feed

  • Question


    When using the following VB.Net code to pull the UserID from the Membership provider and then use the GUID in a where clause I get the following error:


    Character literal must contain exactly one character

    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.Web.Query.Dynamic.ParseException: Character literal must contain exactly one character


    Here is the code that I'm using to pull the data:


    Dim UserGuid As Guid = DirectCast(Membership.GetUser.ProviderUserKey, Guid)

    LinqSalesCustomers.Where = String.Format("UserID = '{0}'", TypeDescriptor.GetConverter(UserGuid).ConvertTo(UserGuid, GetType(String)))


    I've tried every combination of single quotes, braces {}, etc. and can't get this to work. Also, when I use a data context object I can set db.log equal to a TextWriter object and see the SQL that Linq is generating but I can't seem to figure out how to debug Linq to SQL when I use a LinqDataSource. How do I access the datacontext object that the LinqDataSource is connecting to so I can set the log output?


    Thanks in advance for any help.

    Friday, September 26, 2008 2:03 PM

All replies

  • The Where property on the LINQ data source uses a mini expression language that is not VB.  Single quote literal are character literals, so you can only put one character between them. I don't recall that there is a guid literal you can use.  However, the LINQ data source has a built-in mechanism for handling parameters for these expressions.  Youl should not have to be using string.Format to build an expression string with the guid in it.


    You should be able to do something like this:


    LinqSalesCustomers.Where = "UserID = @id";


    LinqSalesCustomers.WhereParameters.Add("id", UserGuid);



    • Proposed as answer by Ricardo Pinto Friday, April 30, 2010 10:49 AM
    Sunday, September 28, 2008 2:27 AM
  • Hello!


    Thanks Matt, you solved my problem!




    Friday, April 30, 2010 10:50 AM