Search code examples
c#asp.netweb.net-framework-version

How to use sqldatasource by showing only preferred data using session?


the usual way for me to do sqldatasource is this. Which just normally shows all forums in a table format.

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>"
    SelectCommand="SELECT [forum_id], [forum_topic], [forum_description], [forum_status], [company_name], [replies_count], [publish_date], [publisher_name], [forum_img_link], [moderator_name] FROM [forum_accept_tbl]">
</asp:SqlDataSource>

However, I want to show not all forum, but only one forum with session forum id that I've got. Below is the code that didn't work for me.

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>" 
    SelectCommand="SELECT [forum_id], [forum_topic], [forum_description], [forum_status], [company_name], [replies_count], [publish_date], [publisher_name], [forum_img_link], [moderator_name] FROM [forum_accept_tbl] WHERE [Forum_id] = Session["Forum_id"]"></asp:SqlDataSource>

I tried to add WHERE but it didn't recognize the session. This is on my forum.aspx code btw. Should it be changed in forum.aspx.cs or not that necessary? I'm still new to asp.net and I think this is a simple mistake but I hope I can learn more.

EDITED CURRENT PROGRESS. After reading some useful documents suggested. Below is my current progress with the current error it shows.

                     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>" SelectCommand="SELECT [forum_id], [forum_topic], [forum_description], [replies_count], [forum_img_link], [company_name], [publisher_name], [publish_date], [moderator_name] FROM [forum_accept_tbl] WHERE ([forum_id] = @forum_id)"> <SelectParameters> <asp:SessionParameter Name="forum_id" SessionField="forum_id" Type="Int32" /> </SelectParameters></asp:SqlDataSource>
                     <div class="col">
                        <asp:GridView class="table table-striped table-bordered" ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
                           <Columns>

I've checked the session and it works well (taking the right session forum id). however, after i run it and click for the link button, it shows this error below:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'forum_aspx' does not contain a definition for 'GridView2_SelectedIndexChanged' and no accessible extension method 'GridView2_SelectedIndexChanged' accepting a first argument of type 'forum_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error: Line 44



Line 42:                      <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>" SelectCommand="SELECT [forum_id], [forum_topic], [forum_description], [replies_count], [forum_img_link], [company_name], [publisher_name], [publish_date], [moderator_name] FROM [forum_accept_tbl] WHERE ([forum_id] = @forum_id)"> <SelectParameters> <asp:SessionParameter Name="forum_id" SessionField="forum_id" Type="Int32" /> </SelectParameters></asp:SqlDataSource>
Line 43:                      <div class="col">
Line 44:                         <asp:GridView class="table table-striped table-bordered" ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
Line 45:                            <Columns>
Line 46:                               <asp:BoundField DataField="forum_id" HeaderText="forum_id" ReadOnly="True" SortExpression="forum_id" InsertVisible="False" >

So in my case, I took session forum id ( which is '14'), and to my expectation, it should have taken 14 from the database and shows it on the page. But instead, there is this error. can someone please explain? I've read some of the documents as well and I followed each step. I'm still new and trying to learn whatever I can.


Solution

  • Don't need to do this in code, you can write your sql like this:

    WHERE [Forum_id] = @fid">
    

    And then declare the parameters within the SqlDataSource (where you currently have ><

        <SelectParameters>
        <asp:SessionParameter Name="fid" SessionField="Forum_ID" Type="Int32" />
    </SelectParameters>
    

    See https://learn.microsoft.com/en-us/previous-versions/aspnet/z72eefad(v=vs.100)