Obtaining a Parameter from a Session Variable

Obtaining a Parameter from a Session Variable

In ASP.NET, you can obtain a parameter from a Session variable by using the SessionParameter object in the SelectParameters collection of the SqlDataSource control. Here are the steps:

Define a parameter in your markup, for example:

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>”

     SelectCommand=”SELECT * FROM Orders WHERE CustomerID = @CustomerID”>

     <SelectParameters>

         <asp:SessionParameter Name=”CustomerID” SessionField=”CustomerId” Type=”String” />

     </SelectParameters>

</asp:SqlDataSource>

Set the SessionField property of the SessionParameter object to the name of the Session variable that contains the value you want to use as the parameter:

<asp:SessionParameter Name=”CustomerID” SessionField=”CustomerId” Type=”String” />

In this example, the Name property of the parameter is set to “CustomerID”, which is the name of the parameter in the SQL query. The SessionField property of the SessionParameter object is set to “CustomerId”, which is the name of the Session variable that contains the value you want to use as the parameter.

Assign a value to the Session variable in your code behind:

Session[“CustomerId”] = “ALFKI”;

Run your application to see the results. Now, when the SqlDataSource control is executed, it will use the value of the Session variable as the value of the “CustomerID” parameter in the query. You can change the value of the Session variable at runtime, and the SqlDataSource control will use the new value as the value of the parameter.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Financial Market, Participants and Instruments
Passing a Parameter on a query string

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?