asp:SqlDataSource to DataSet Items

flavour404 picture flavour404 · Jun 24, 2009 · Viewed 21.7k times · Source

I have an asp:SqlDataSource ID="SqlDataSource1" tool on my aspx page, what I want to do in the c# sharp code behind is transfer the records returned by the datasource and put them into a DataSet, so that I can add paging to my page, how do I do that, my attempts so far have failed?!

My attempts so far have been along the lines of:

DataSet Items = new DataSet(); Items = SqlDataSource1.Data();

But the error I am getting is that the SqlDataSource1 control is not accessible in this context and so obviously the intellisense is not picking it up, so the 'Data()' bit is complete fiction on my part...

Thanks, R

Answer

Faheem picture Faheem · Jun 24, 2009

flavour404, You should not get that error if you have set up the control properly. I just tested your scenario and it works with out the error you have mentioned.

SqlDataSource1 does not have any Data method, you might be looking for Select() method and it does not return DataSet. If you set SqlDataSource.DataSourceMode property to 'DataSet' you would get DataView object. See the sample below

<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet"
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
            SelectCommand="SELECT * FROM [Readings]"></asp:SqlDataSource>

DataView testView = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);

Read MSDN for details:

http://msdn.microsoft.com/en-us/library/dz12d98w.aspx
http://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

Hope this helps!