GridView but only displaying the rows for the id in the url

Xander Todor 81 Reputation points
2021-06-14T14:26:16.2+00:00

I have a table (tblUsers) displayed by a GridView with an Edit button, where the Edit button binds the UserId to the URL, so it redirects to that specific user (~../Users/UserEdit.aspx?UserId=25)

Now, tblUsers is connected with tblPayments through UserId so I would like to show a GridView of tblPayments but only those rows for the specific UserId in the URL.

I have successfully created the GridView but I am displaying the whole table for every Id, so I am not sure how to filter it. This is my method so far:

 private GridView gvPayments;
    protected void Filter(int source)
    {
        if (HttpContext.Current.User.IsInRole("Administrator"))
        {
            string PaymentId = string.Empty;

            string queryBase = "SELECT * FROM [tblPayments] WHERE PaymentId IS NOT NULL ";
            string queryWhere = string.Empty; // if I hardcode where PaymentId=25 it works
            string queryOrder = " ORDER BY [PayeeId]";


            // FINDCONTROL
            sdsPayments.SelectCommand = queryBase + queryWhere + queryOrder;
            gvPayments = FormView_Users.Row.FindControl("gvPayments") as GridView;

        }
    }

I call this method within PageLoad and set it to 0. This is the aspx page, with reduced columns for readability.

<asp:GridView ID="gvPayments" runat="server" AutoGenerateColumns="False" DataKeyNames="PaymentId" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="PaymentId" HeaderText="PaymentId" ReadOnly="True" SortExpression="PaymentId" />
        </Columns>
    </asp:GridView>
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,455 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,076 Reputation points
    2021-06-15T07:35:24.513+00:00

    Hi @Xander Todor ,
    As far as I think,you could use Request.QueryString to get the value from the url. Just like this:

    string value = Request.QueryString("UserId");  
    

    And you could filter with the UserId.

    string SelectQuery="Select * from [tblPayments] where PaymentId='"+ value +"' order by [PayeeId]";  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.