locked
How to pass a parameter to the query of the second page that using GridView. RRS feed

  • Question

  • User899592849 posted

    Hello,

    I am trying pass a parameter from a textbox on page1.aspx to the second page page2.aspx that using <asp:GridView /><asp:SqlDataSource />.  Looks like I am able to pass the value onto the second page but not into the query on SqlDataSource.  please help. Thank you.

    page1.aspx
    
    <asp:TextBox ID="txtRndID" runat="server" Columns="40" Visible="false" >Text</asp:TextBox>
    <asp:Button ID="btnGuest" runat="server" Text="Guests" Visible="true" />
    
    Public Sub btnGuest_Click(sender As Object, e As EventArgs) Handles btnGuest.Click
            Response.Redirect(“page2.aspx?Parameter=” + Server.UrlEncode(txtRndID.Text))
    End Sub
    
    -----------------------------------
    page2.aspx
    
    <form id="form1" runat="server">
        <div>
        
        <asp:GridView
            id="grdMovies"
            DataSourceID="srcMovies"
            DataKeyNames="RequestID"
            AutoGenerateEditButton="true"
            AutoGenerateDeleteButton="true"
            Runat="server" />
    
             <asp:SqlDataSource id="srcMovies" runat="server"
            ConnectionString="<%$ ConnectionStrings:StringABC %>"
            SelectCommand="SELECT RequestID, first_name, last_name FROM tbl_requestguest1 where RNDGuest_ID = '%str%'"
            UpdateCommand="UPDATE tbl_requestguest1 SET first_name=@first_name, last_name=@last_name
                WHERE RequestID=@RequestID">
            <selectparameters>
    		    <asp:sessionparameter name="RNDGuest_ID" sessionfield="RNDGuest_ID" />
    	    </selectparameters>
                
            </asp:SqlDataSource> 
        </div>
        </form>
    
    page2.aspx.cs - code behind of page2
    
    protected void Page_Load(object sender, EventArgs e)
        {
    
            String str;
            str = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
            this.Session["RNDGuest_ID"] = Server.UrlDecode(Request.QueryString["Parameter"].ToString());
            str = Session["RNDGuest_ID"].ToString();
    
        }

    Friday, February 1, 2019 4:53 PM

Answers

  • User-943250815 posted

    Since you are using SQLDatasource, on page2.aspx:
    1) Switch to Design Mode
    2) Mouse over on SQLDatasource then click in ">" signal
    3) Select "Configure DataSource"
    4) Click Next button
    5) Click "Where" button
    6) Now select Column, Operator and Source = "QueryString"
    7) On Querystring field use "Parameter"
    8) Click Add button
    9) You ready to go

    Do not forget to remove existing SQL Expression in box Where clause

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 2, 2019 2:01 AM

All replies

  • User-943250815 posted

    Since you are using SQLDatasource, on page2.aspx:
    1) Switch to Design Mode
    2) Mouse over on SQLDatasource then click in ">" signal
    3) Select "Configure DataSource"
    4) Click Next button
    5) Click "Where" button
    6) Now select Column, Operator and Source = "QueryString"
    7) On Querystring field use "Parameter"
    8) Click Add button
    9) You ready to go

    Do not forget to remove existing SQL Expression in box Where clause

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 2, 2019 2:01 AM
  • User-2054057000 posted

    You set the Query for SqlDataSource like this:

    srcMovies.SelectCommand="SELECT RequestID, first_name, last_name FROM tbl_requestguest1 where RNDGuest_ID = '"+QueryString["Parameter"].ToString()+"'";

    Saturday, February 2, 2019 11:01 AM
  • User-168407370 posted

     Please Remove From page2.aspx.cs page. i thinks variable overright

            " str = Server.UrlDecode(Request.QueryString["Parameter"].ToString()); "
    
    Monday, February 4, 2019 5:10 AM