Last post Jul 15, 2014 04:27 AM by Terry Guo - MSFT
Jul 11, 2014 11:30 PM|vstorpedo|LINK
I do have a data source code that look like this
<asp:SqlDataSource ID="AdBox_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:MainWebConnection %>"
SelectCommand="SELECT DISTINCT [TCompanyName], [ImageUrl], [NavigateUrl], [Keyword], [TEnterDate], [TDoHavePic] FROM [AdList] WHERE (([TDoHavePic] = @TDoHavePic) AND ([Keyword] = @Keyword)) ORDER BY [TEnterDate]" OnSelecting="AdBox_SqlDataSource_Selecting">
<asp:Parameter DefaultValue="YesPic" Name="TDoHavePic" Type="String" />
<asp:Parameter DefaultValue="Ad Box" Name="Keyword" Type="String" />
I would like to know if there is a way to get the result of this Select Command and save it to a Data Reader in the code behind like this:
Dim reader As SqlDataReader = "Result from database search go here"
Jul 11, 2014 11:45 PM|a2h|LINK
I guess you want to read the value from selecct command in your SQLDatasource. if so then you can use the DataView method.
Please try with the sample code:
'Assign the datasource select values to dataview
Dim dvSql As DataView = DirectCast(AdBox_SqlDataSource.[Select](DataSourceSelectArguments.Empty), DataView)
'Loop through the values and read each values
For Each drvSql As DataRowView In dvSql
Dim value As String = drvSql("TCompanyName").ToString()
Jul 11, 2014 11:59 PM|vstorpedo|LINK
Yes A2H that is what I want and thanks for your answer. On the other hand I was trying to do the same with a DataReader, since I read that it is more efficient and more powerful. Couple questions: 1) Is it possible to do it with the DataReader or the only
way to do it is with the DataView? 2) If it can be done with the DataReader, what is the way? I am just asking these two questions for curiosity and education. Finally, if in your opinion the best way and the only option is the DataView, I will do it this
way. Thanks once again for your help.
Jul 15, 2014 04:27 AM|Terry Guo - MSFT|LINK
As I know we can't convert a datasource to a reader. A reader only works with an open database connection.
So, please try to use solution of A2H.