Here is the ListView I have for the products page:

<asp:ListView ID="list_view" runat="server" DataSourceID="SqlDataSource1">

    <ItemTemplate>
        <table class="single_product">
            <tr>
                <td>
                    <img src="<%# Eval("image") %>" />
                </td>
                <td>
                    <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
                </td>
                <td>
                    <asp:Label ID="descriptionLabel" runat="server" Text='<%# Eval("description") %>' />
                </td>
                <td>
                    <form action="comments.aspx" method="post">
                        <input type="hidden" name="prod_id" value="<%# Eval("prod_id") %>">
                        <input type="submit" name="prod_submit" class="button" value="View Comments">
                    </form>
                </td>
            </tr>
        </table>
    </ItemTemplate>

    </asp:ListView>

The user can view the comments for a certain product by clicking the button, taking the prod_id over to the comments page.

Now here is the datasource for the comments page:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:username %>" 
        ProviderName="<%$ ConnectionStrings:username.ProviderName %>"
        InsertCommand="INSERT INTO comment (prod_id, comment_desc, username) VALUES (@prod_id, @comment_desc, @username)"
        SelectCommand="SELECT * FROM comment WHERE prod_id = prod_id ORDER BY comment_date DESC">
    <InsertParameters>
        <asp:Parameter Name="prod_id" Type="Int32" DefaultValue="2" />
        <asp:Parameter Name="comment_desc" Type="String"/>
        <asp:Parameter Name="username" Type="String" />
    </InsertParameters>
    </asp:SqlDataSource>

I want the select to be like this:

SelectCommand="SELECT * FROM comment WHERE prod_id = [value from previous page] ORDER BY comment_date DESC"

I have used Request.Form, etc. but no luck.

I also have other problems but I'll mention them once/if this is solved.

Thank you for any responses.

Dani AI

Generated

The core problem is that the comments page never receives a proper SQL parameter, so the WHERE clause doesn't filter anything. 's page posts the product id but the comments query must use a parameter (for example @prod_id) and that parameter must be populated before the SqlDataSource performs its Select. 's Session idea will work, but there are simpler, more transparent options.

Easiest, recommended change: pass the id on the query string and let the SqlDataSource read it. Example (only the relevant parts shown):

SelectCommand="SELECT * FROM comment WHERE prod_id = @prod_id ORDER BY comment_date DESC"

<SelectParameters>
  <asp:QueryStringParameter Name="prod_id" QueryStringField="prod_id" Type="Int32" />
</SelectParameters>

Make the product link emit a URL like comments.aspx?prod_id=123 (use a HyperLink or set NavigateUrl with Eval) instead of relying on a nested HTML form POST.

If POSTing is required, use a FormParameter or set the parameter in code-behind before binding:

<SelectParameters>
  <asp:FormParameter Name="prod_id" FormField="prod_id" Type="Int32" />
</SelectParameters>

or in Page_Load:

commentsSource.SelectParameters["prod_id"].DefaultValue =
    Request.Form["prod_id"] ?? Request.QueryString["prod_id"];
commentsRepeater.DataBind(); // bind after you set the parameter if needed

Troubleshooting tips: confirm the incoming value by temporarily writing Request.Form["prod_id"] / Request.QueryString["prod_id"] to the page, ensure parameter name matches the @ placeholder exactly, set Type=Int32 to avoid conversion errors, and avoid nested server forms (use a link or server-side redirect). For inserting comments, map the prod_id the same way (QueryString/Form/Session) into the InsertParameters or set it programmatically so the inserted row links to the correct product.

Try using Session. (C#)

[B]PRODUCTS.aspx[/B]
private void Page_Load(object sender, System.EventArgs e)
    {
         Session["prod_id"] = "value1";
    }

[B]COMMENTS.aspx[/B]
private void Page_Load(object sender, System.EventArgs e)
    {
        string prod_id = (string)(Session["prod_id"]);
    }



<SelectParameters>            
<asp:SessionParameter SessionField="prod_id" DbType="String" Name="Product ID" />   
</SelectParameters>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.