Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataSet'

5 posts, 0 answers
  1. L
    L avatar
    268 posts
    Member since:
    Apr 2008

    Posted 08 Oct 2011 Link to this post

    hi

    I have this casting of dataset issues and i just could not get it working. please help and any inconvience caused regretted. Thanks

    Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataSet'.
     
    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    If e.CommandName = "select" Then
    Session("sessiondataset") = CType(Session("sessiondataset"), DataSet).Tables("CTETable").Select("Customer ='" + DirectCast(e.CommandSource, LinkButton).Text + "'")
     RadGrid1.Rebind()
    end if
    End Sub

    Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
            RadGrid1.DataSource = Session("sessiondataset")
     End Sub

     

  2. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3375 posts
    Member since:
    Sep 2012

    Posted 09 Oct 2011 Link to this post

    Hi L,

    reviewing your code I find the following issue: First you are casting the Session("sessiondataset") object to DataSet. Then you are assigning it an object of time DataTable. So the second time the code is called the Session("sesiondataset") object holds a DataTable which you are trying to cast to DataSet and if that is the case, it is rather expected to receive the mentioned exception. I assume that you intended to use different Session object, like Session("sessiondataset") and Session("sessiondatable"), right?

    Kind regards,
    Iana Tsolova
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

  3. L
    L avatar
    268 posts
    Member since:
    Apr 2008

    Posted 10 Oct 2011 Link to this post

    hi

    I assume that you intended to use different Session object, like Session("sessiondataset") and Session("sessiondatable"), right?

    No, I am not trying to use a different session object

    As you can see from my code, i am trying to filter the data in the grid which is using session object in the RadGrid1_ItemCommand and NeedDataSource

    Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    If e.CommandName = "select" Then
    Session("sessiondataset") = CType(Session("sessiondataset"), DataSet).Tables("CTETable").Select("Customer ='" + DirectCast(e.CommandSource, LinkButton).Text + "'")
     RadGrid1.Rebind()
    end if
    End Sub


     

     

    Protected Sub RadGrid1_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource

     RadGrid1.DataSource = Session("sessiondataset")

     

    End Sub

     

    I have looked through the examples on grid filtering but there isn't sample to show how to filter grid data when i am using session dataset in the grid.

    Thanks and hope i am not giving you any inconveniences.

  4. Iana Tsolova
    Admin
    Iana Tsolova avatar
    3375 posts
    Member since:
    Sep 2012

    Posted 10 Oct 2011 Link to this post

    Hi L,

    I see what you mean. However such approach cannot work as expected because you are using the same object for holding different types in it. You should either use different objects or move the code for filtering the DataSource in the NeedDataSource event.

    All the best,
    Iana Tsolova
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

  5. L
    L avatar
    268 posts
    Member since:
    Apr 2008

    Posted 11 Oct 2011 Link to this post

    hi

    Are there any example on how to filter data in the NeedDataSource method? thanks

Back to Top