views:

1545

answers:

1

Hi,

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

+1  A: 

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!

Faheem
Ok, that makes sense, but in my C# code behind file I still get the error SqlDataSource1 does not exist in the current context.How do I reference the <asp:SqlDataSource ID="SqlDataSource1" runat="server"DataSourceMode="DataSet"...> which is on my search_results.aspx page?Thanks.
flavour404
Oh, and thanks for the above, that was really helpful.
flavour404
That is weird. Are you sure that you are editing the right code behind file? Try accessing other controls from aspx page in your code behind. You may also want to verify the ID's of control that you are accessing. Try building the code and remove any build errors. Fixing building errors should bring back the intellisense.
Faheem
Ah, I found the error, I had removed the "Inherits" keyword from the page definition and hence it was picking up the on load event from higher up...There is so much going on.
flavour404