2

Good morning!
I am using an EntityDataSource and would like to filter it with a WhereParameter that is a property of a object stored in a session variable, Session("Ticket"). Is this possible?

Here's the class I use for a Ticket object that gets stored into a session variable

Partial Public Class TICKET
    Private _ID As Integer              '-- I want to use the property "ID" as the parameter
    Private _Category As String     
    Private _Status As String
    Private _Priority As String
    ...
    ...
End Class

and here is some markup, but not sure how to get the property "ID" in as the parameter

<asp:EntityDataSource ID="edsDBase" runat="server"  AutoGenerateWhereClause="True"
     ConnectionString="name=edsDBContainer"
     DefaultContainerName="edsDBContainer" EnableFlattening="False"
    EntitySetName="edsTicket" EnableDelete="True" EnableInsert="True"
    EnableUpdate="True" EntityTypeFilter="tbICObjectBase">
       <WhereParameters>
         <asp:SessionParameter SessionField="Ticket.ID (this is what I'd like to do)" Name="TicketID" Type="Int32" />
       </WhereParameters>
</asp:EntityDataSource>

I also tried doing it programmatically through the OnSelecting event of the EntityDataSource but got confused on that, too.

Protected Sub edsTickets_OnSelecting(sender As Object, e As EntityDataSourceSelectingEventArgs)
    Dim eds As EntityDataSource = CType(sender, EntityDataSource)
    Dim ticket As V_TICKETS = Session("Ticket")
    Dim xparam As New Parameter("ID", DbType.Int32)
    xparam.DefaultValue = ticket.ID
    eds.WhereParameters.Add(xparam)
End Sub

But this threw an exception saying I cannot cast an EntityDataSourceView to an EntityDataSource. Anyone able to help me straighten this out? Thank you!

Carlos Mendieta
  • 860
  • 16
  • 41
  • 1
    `Dim eds As EntityDataSource = e.DataSource` 'CType(sender, EntityDataSource) – G3nt_M3caj Jun 01 '21 at 16:29
  • Gent, would love to give you credit for this if you post as an answer. I ended up using your provided statement and got my problem solved in the code-behind. – Carlos Mendieta Jun 07 '21 at 18:52
  • 1
    Hey Carlos, glad to hear that. My purpose is that to improve my English and most important to help beginners not to increase my votes ;). Anyway You can make an answer by your self then you can accept your answer as well. Stay safe bro ;) – G3nt_M3caj Jun 08 '21 at 07:24

1 Answers1

0

My problem was solved by handling the OnSelecting event from the EntityDataSource.

Protected Sub edsTickets_OnSelecting(sender As Object, e As EntityDataSourceSelectingEventArgs)
    Dim eds As EntityDataSource = e.DataSource 'As suggested by G3nt_M3caj on Jun 1 at 16:29. Thanks again!
    Dim ticket As V_TICKETS = Session("Ticket")
    Dim xparam As New Parameter("ID", DbType.Int32)
    xparam.DefaultValue = ticket.ID
    eds.WhereParameters.Add(xparam)
End Sub
Carlos Mendieta
  • 860
  • 16
  • 41