locked
VB Dynamic Queries - Client DataServiceQuery RRS feed

  • Question

  •  

    Hi
    I have looked at the post ( http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/a1743153-c601-4dfc-8507-76396b05c8a2/  ) and it looks like exactly what I am trying to archive however when converting the spinets to VB I get nothing returned from

    c#    “Type EntityType = properties[1].GetValue(asyncResult.AsyncState) as Type “.
    VB    “Dim EntityType As Type = TryCast(properties(1).GetValue(asyncResult.AsyncState, Nothing), Type)”

    I an not sure if I have missed something ? Please find below a code example;

    Hope someone can help.

    Regards,

    Richard C





    Partial Public Class Data_Control

    Inherits UserControl

    Public Data_Entity As Type

    Public MyDataService As Data_Service.TrickyposEntities

    Private Delegate Sub Load_Done(ByVal asyncResult As IAsyncResult)

    Private LoadCallBack As AsyncCallback

    Public Sub New()

    InitializeComponent()

    MyDataService = New Data_Service.TrickyposEntities(New Uri("WebDataService1.svc", UriKind.RelativeOrAbsolute))

    End Sub

    Private Sub Load_Done_CallBack(ByVal asyncResult As IAsyncResult)

    Dim properties As PropertyInfo() = asyncResult.AsyncState.[GetType]().GetProperties()

    'Get the Query From the State

    Dim dsQuery As Object = properties(0).GetValue(asyncResult.AsyncState, Nothing)

    'Get the Entity Type for the query

    Dim EntityType As Type = TryCast(properties(1).GetValue(asyncResult.AsyncState, Nothing), Type)

    'Get the DataServiceQuery<T> Type

    Dim dataServiceQueryType As Type = GetType(DataServiceQuery(Of )).MakeGenericType(EntityType)

    'Get the EndExecute method on the DataServiceQuery<T> instance

    Dim endExecuteMethod As MethodInfo = dataServiceQueryType.GetMethod("EndExecute")

    'Execute the EndExecute method on the DataServiceQuery<T> instance

    Dim results As Object = endExecuteMethod.Invoke(dsQuery, New Object() {asyncResult})

    End Sub

    Private Sub Generic_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    'Set Data_Entity Here For Example

    Data_Entity = GetType(Pos_Menu)

    Dim dsQuery As DataServiceQuery = CreateQueryRunTime(Data_Entity.Name, Data_Entity, MyDataService)

    Dim dsQuery_Method As MethodInfo = dsQuery.[GetType]().GetMethod("BeginExecute")

    Dim LoadCallBack = New AsyncCallback(AddressOf Load_Done_CallBack)

    Dim Method_Params() As Object = {LoadCallBack, dsQuery}

    dsQuery_Method.Invoke(dsQuery, Method_Params)

    End Sub

    Function CreateQueryRunTime(ByVal entitySetName As String, ByVal entityType As Type, ByRef ctx As DataServiceContext) As DataServiceQuery

    Dim execMethod As MethodInfo = ctx.[GetType]().GetMethod("CreateQuery", BindingFlags.Instance Or BindingFlags.[Public])

    Dim execGenericMethod As MethodInfo = execMethod.MakeGenericMethod(entityType)

    Return TryCast(execGenericMethod.Invoke(ctx, New Object() {entitySetName}), DataServiceQuery)

    End Function

    End Class

    • Changed type trickypos Sunday, March 8, 2009 4:57 PM
    Sunday, March 8, 2009 1:35 PM

Answers

  • Hi ,
     The AsyncState is supposed to carry the state over from the caller to the callback .
    in your sample , you are not passing the Type variable to be able to extract it in the callback

    Change

    Dim Method_Params() As Object = {LoadCallBack, dsQuery}
    to


    Dim Method_Params() As Object = {LoadCallBack,

    New

     

    With {Key .Query = dsQuery, Key .EntityType = Type }

    }

    Anonymous types are hard to debug , I'd suggest moving to a strongly typed object to hold the state . That should help make debugging problems like this easier.

    Phani Raj Astoria
    Saturday, March 28, 2009 7:44 PM
    Moderator