Unable to cast object of type 'System.Web.UI.WebControls.SqlDataSource' to type 'System.Collections.

Discussion in 'ASP .Net' started by Chris, Feb 5, 2007.

  1. Chris

    Chris Guest

    Hi,

    I want to limit the amount of data shown in a page coming from a database.
    Everything works except that I get the error:
    "Unable to cast object of type 'System.Web.UI.WebControls.SqlDataSource' to
    type 'System.Collections.IEnumerable'"
    on line: PageDataSource.DataSource = SqlDataSource2

    Thanks for help


    code-behind:
    -----------
    Imports System.Data
    Imports System.Data.OleDb

    Partial Class Item
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles Me.Load
    Dim PageDSource As New PagedDataSource()
    PageDSource.DataSource = SqlDataSource2
    PageDSource.AllowPaging = True
    PageDSource.PageSize = 6
    End Sub
    End Class


    aspx file:
    ---------
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
    ConnectionStrings:prod %>"
    SelectCommand="SELECT * FROM [Prod] WHERE ([ItemId] = @ItemId)">
    <SelectParameters>
    <asp:QueryStringParameter Name="CId" DefaultValue="1"
    QueryStringField="ite" />
    </SelectParameters>
    </asp:SqlDataSource>
    Chris, Feb 5, 2007
    #1
    1. Advertising

  2. Chris

    Teemu Keiski Guest

    SqlDataSource isn't itself enumerable or a resultset, it's a "middle-man"
    control to get you the resultset.

    You would need to get the resultset out of the SqlDataSource by calling it's
    Select method (with arguments), nad passing this returned object to the
    DataSource property.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net



    "Chris" <> wrote in message
    news:...
    > Hi,
    >
    > I want to limit the amount of data shown in a page coming from a database.
    > Everything works except that I get the error:
    > "Unable to cast object of type 'System.Web.UI.WebControls.SqlDataSource'
    > to type 'System.Collections.IEnumerable'"
    > on line: PageDataSource.DataSource = SqlDataSource2
    >
    > Thanks for help
    >
    >
    > code-behind:
    > -----------
    > Imports System.Data
    > Imports System.Data.OleDb
    >
    > Partial Class Item
    > Inherits System.Web.UI.Page
    >
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles Me.Load
    > Dim PageDSource As New PagedDataSource()
    > PageDSource.DataSource = SqlDataSource2
    > PageDSource.AllowPaging = True
    > PageDSource.PageSize = 6
    > End Sub
    > End Class
    >
    >
    > aspx file:
    > ---------
    > <asp:SqlDataSource ID="SqlDataSource2" runat="server"
    > ConnectionString="<%$ ConnectionStrings:prod %>"
    > SelectCommand="SELECT * FROM [Prod] WHERE ([ItemId] = @ItemId)">
    > <SelectParameters>
    > <asp:QueryStringParameter Name="CId" DefaultValue="1"
    > QueryStringField="ite" />
    > </SelectParameters>
    > </asp:SqlDataSource>
    >
    Teemu Keiski, Feb 12, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page