asp:SqlDataSource to DataSet Items

只愿长相守 提交于 2020-01-02 17:48:31

问题


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:


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!




回答2:


This Should work! :)


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

DataTable table = view.ToTable();
DataSet ds = new DataSet();
ds.Tables.Add(table);


来源:https://stackoverflow.com/questions/1035714/aspsqldatasource-to-dataset-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!