GridView DataSource Is Nothing

Discussion in 'ASP .Net' started by SAL, Jul 31, 2008.

  1. SAL

    SAL Guest

    I am trying to cast my Gridview in my Code behide as a Dataset so I can
    manipulate the data. Here is the code snippet:

    Dim FilteredDataSet As New System.Data.DataSet

    Dim gv As New GridView
    gv = CType(tbl_ReportData.FindControl("GridView_1"), GridView)
    gv.GridLines = GridLines.Both
    gv.DataBind()
    FilteredDataSet = DirectCast(gv.DataSource, System.Data.DataSet)


    Now the issue I'm running into is the gv.DataSource is Nothing. My
    GridView_1 control which I assign my gv to is bound to a <asp:SqlDataSource
    ID="SqlDataSource1> on the HTML side and loads GridView_1 fine. Even if I
    assign the gv.DataSource as "gv.DataSource =
    CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSource", the
    DataSource still shows nothing. When I check
    CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSourceID the is
    shows SqlDataSource1.

    Can anyone tell why my GridView_1 gets populated just fine but the
    DataSource shows NOTHING, which is preventing me from creating a DataSet from
    my GridView? If my syntax is wrong can you show me how to fix it?

    Thanks,
     
    SAL, Jul 31, 2008
    #1
    1. Advertising

  2. SAL

    Munna Guest

    Hi,

    First assign a dataset as a data source of the grid..
    then try to cast...

    Regards

    Munna
     
    Munna, Jul 31, 2008
    #2
    1. Advertising

  3. SAL

    Ben Schwehn Guest

    > Dim gv As New GridView

    I believe this should better be

    Dim gv As GridView


    > gv = CType(tbl_ReportData.FindControl("GridView_1"),


    > Can anyone tell why my GridView_1 gets populated just fine but the
    > DataSource shows NOTHING, which is preventing me from creating a DataSet
    > from my GridView?


    Possibly because you're trying to access the DataSource property before it
    has been set. Is it still nothing in say Page_PreRender or
    GridView_1_OnDataBound?



    --
    Ben | bschwehn.de
     
    Ben Schwehn, Jul 31, 2008
    #3
  4. SAL

    SAL Guest

    Yes, the PreRender shows it as NOTHING. I Changed the declaration to Dim gv
    as GridView as you suggested but it didn't work.

    "Ben Schwehn" wrote:

    > > Dim gv As New GridView

    >
    > I believe this should better be
    >
    > Dim gv As GridView
    >
    >
    > > gv = CType(tbl_ReportData.FindControl("GridView_1"),

    >
    > > Can anyone tell why my GridView_1 gets populated just fine but the
    > > DataSource shows NOTHING, which is preventing me from creating a DataSet
    > > from my GridView?

    >
    > Possibly because you're trying to access the DataSource property before it
    > has been set. Is it still nothing in say Page_PreRender or
    > GridView_1_OnDataBound?
    >
    >
    >
    > --
    > Ben | bschwehn.de
    >
     
    SAL, Jul 31, 2008
    #4
  5. SAL

    SAL Guest

    The problem is, I can't assign the DataSet to the GridView_1 DataSource
    because CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSource
    also returns NOTHING. GridView_1 gets rendered because it's DataSourceID is
    assigned to <asp:SqlDataSource ID="SqlDataSource1>.

    "Munna" wrote:

    > Hi,
    >
    > First assign a dataset as a data source of the grid..
    > then try to cast...
    >
    > Regards
    >
    > Munna
    >
     
    SAL, Jul 31, 2008
    #5
  6. SAL

    SAL Guest

    I'm not 100% certain of this, but is it possible that when you set the
    DataSourceID on your Gridview to a <asp:SqlDataSource> or
    <asp:ObjectDataSource> that the your DataSource of your Gridview will return
    NOTHING?

    "SAL" wrote:

    > I am trying to cast my Gridview in my Code behide as a Dataset so I can
    > manipulate the data. Here is the code snippet:
    >
    > Dim FilteredDataSet As New System.Data.DataSet
    >
    > Dim gv As New GridView
    > gv = CType(tbl_ReportData.FindControl("GridView_1"), GridView)
    > gv.GridLines = GridLines.Both
    > gv.DataBind()
    > FilteredDataSet = DirectCast(gv.DataSource, System.Data.DataSet)
    >
    >
    > Now the issue I'm running into is the gv.DataSource is Nothing. My
    > GridView_1 control which I assign my gv to is bound to a <asp:SqlDataSource
    > ID="SqlDataSource1> on the HTML side and loads GridView_1 fine. Even if I
    > assign the gv.DataSource as "gv.DataSource =
    > CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSource", the
    > DataSource still shows nothing. When I check
    > CType(tbl_ReportData.FindControl("GridView_1"), GridView).DataSourceID the is
    > shows SqlDataSource1.
    >
    > Can anyone tell why my GridView_1 gets populated just fine but the
    > DataSource shows NOTHING, which is preventing me from creating a DataSet from
    > my GridView? If my syntax is wrong can you show me how to fix it?
    >
    > Thanks,
     
    SAL, Jul 31, 2008
    #6
  7. SAL

    Ben Schwehn Guest

    On Thu, 31 Jul 2008 11:11:01 -0700, SAL wrote:

    > Yes, the PreRender shows it as NOTHING.


    Right, sorry.
    When you're using a SqlDataSource and bind it via the DataSourceId, the
    DataSource property just is not set. Also there isn't even a DataSet
    involved, that you could get.

    But you can get a *DataView* by calling the Select method on your
    SqlDataSource like

    TheSqlDataSource.Select(DataSourceSelectArguments.Empty);

    As an alternative you could always just use old style databinding w/o
    DataSourceId.


    > I Changed the declaration to
    > Dim gv as GridView as you suggested but it didn't work.


    That was an unrelated problem, your code just created a new GridView
    object for no good reason.
     
    Ben Schwehn, Jul 31, 2008
    #7
  8. SAL

    SAL Guest

    Thanks for the info Ben it makes sense now. You are right, having "As New
    GridView" was overkill but I had it there because I thought maybe I was
    missing something which it's clear that I wasn't.

    What did you mean by "As an alternative you could always just use old style
    databinding w/o DataSourceId"? I am assuming you mean to set the DataSource
    of the GridView in the Code behind prior to Rendering the Gridview.


    "Ben Schwehn" wrote:

    > On Thu, 31 Jul 2008 11:11:01 -0700, SAL wrote:
    >
    > > Yes, the PreRender shows it as NOTHING.

    >
    > Right, sorry.
    > When you're using a SqlDataSource and bind it via the DataSourceId, the
    > DataSource property just is not set. Also there isn't even a DataSet
    > involved, that you could get.
    >
    > But you can get a *DataView* by calling the Select method on your
    > SqlDataSource like
    >
    > TheSqlDataSource.Select(DataSourceSelectArguments.Empty);
    >
    > As an alternative you could always just use old style databinding w/o
    > DataSourceId.
    >
    >
    > > I Changed the declaration to
    > > Dim gv as GridView as you suggested but it didn't work.

    >
    > That was an unrelated problem, your code just created a new GridView
    > object for no good reason.
    >
     
    SAL, Jul 31, 2008
    #8
    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.
Similar Threads
  1. Marc Robitaille

    DropDownList.DataSource = Nothing ???

    Marc Robitaille, Jul 14, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,638
    Marc Robitaille
    Jul 14, 2005
  2. Replies:
    1
    Views:
    48,350
    Andrew Robinson
    Dec 27, 2005
  3. jobs
    Replies:
    2
    Views:
    1,692
  4. Pulkit Sharma

    Gridview e.OldValues and e.NewValues are nothing

    Pulkit Sharma, Jul 24, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    1,390
    Rachna
    Nov 19, 2007
  5. Devin Jeanpierre
    Replies:
    2
    Views:
    512
    Devin Jeanpierre
    Feb 14, 2012
Loading...

Share This Page