locked
Is this possible to do with a sqldatasource? RRS feed

  • Question

  • User426001450 posted

    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">
            <SelectParameters>
                <asp:Parameter DefaultValue="YesPic" Name="TDoHavePic" Type="String" />
                <asp:Parameter DefaultValue="Ad Box" Name="Keyword" Type="String" />
            </SelectParameters>
    
        </asp:SqlDataSource>
    

    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"

    Thanks

    Friday, July 11, 2014 11:30 PM

Answers

  • User2103319870 posted

    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() Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 11:45 PM

All replies

  • User2103319870 posted

    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() Next
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 11:45 PM
  • User426001450 posted

    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.     

    Friday, July 11, 2014 11:59 PM
  • User697462465 posted

    Hi vstorpedo,

    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.

    Best Regards,
    Terry Guo

    Tuesday, July 15, 2014 4:27 AM