I have a form with a recordset which is filtered on some controls on the form. In the code behind page I want to loop through the records as filtered on the form. I can set up another SqlDataSource and refilter it on the same controls, but I'm wondering if there's an easier way just to select the existing recordset as filtered.
Thanks,
Ed

Dani AI

Generated

Short answer: yes — server-side controls and nonvisual components placed on the .aspx are generated as fields on the page's partial class and are accessible from the code-behind. For a declarative SqlDataSource the lowest-effort approach is to call its Select method and iterate the returned results instead of creating a second SqlDataSource or rebuilding a DataSet.

var result = SqlDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty);
var dv = result as DataView;    // when DataSourceMode == DataSet
if (dv != null)
{
    foreach (DataRowView row in dv)
    {
        var value = row["ColumnName"];
    }
}
else
{
    var rdr = result as IDataReader;   // when DataSourceMode == DataReader
    while (rdr != null && rdr.Read())
    {
        var value = rdr["ColumnName"];
    }
}

Notes and pitfalls: cast the Select return to the correct type (DataView vs IDataReader) depending on SqlDataSource.DataSourceMode; see the SqlDataSource.Select docs for details (SqlDataSource.Select method (Microsoft Docs)). If parameters are supplied via ControlParameter make sure they are populated before calling Select (usually after Load, e.g. Page.LoadComplete or PreRender). If a GridView is bound via DataSourceID, GridView.DataSource will be null after binding — call the underlying data source's Select or iterate GridView.Rows if you need rendered cell values. and are correct that materializing rows into in-memory objects avoids extra DB calls, but be mindful of memory and ViewState trade-offs. 's assertion that designer-created datasets are unreachable is incorrect — check Form1.aspx.designer.cs for the generated field name and use it directly.

Quick checklist: confirm the control name in Form1.aspx.designer.cs; ensure parameters are set before Select; pick DataView vs IDataReader; use GridView.Rows for rendered values; consider caching (Session/Cache) for repeated work instead of ViewState to avoid bloating the page.

Recommended Answers

All 5 Replies

You can create a custom class to hold a row of data then create a collection to hold all of the custom objects. Then you can traverse the data in any manner you wish without additional trips to the database (depending on the volume).

how about using sqlcommandbuilder and using dataset and data adapter

using a datarow, you can loop through its row.

That would work, too. I just don't use datasets.

Hi folks,
Thanks for the responses. I guess I didn't state the question very well. Thanks to examples on DaniWeb I can loop through a dataset fine. My question is if the form already has a dataset on the designer page (i.e. Form1.aspx), can I access that existing dataset from the code behind page ()? I'm rebuilding another dataset and looping through that fine, I just thought maybe there's an easier way.
Thanks,
Ed

Hey dude
It is not possible to access code behind (.) dataset in front end(.aspx) because dataset is defined in class which inherited from the aspx file
(ie., cs--inherits-->aspx ) you ask like conversely so it is not possible.

if you want
You can declare dataset is aspx file then you can use it in both file(code behind, aspx) or
You declare that globally include to your project then only you can access that in any in that project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.