Export (0) Print
Expand All

SessionParameter Class

Note: This class is new in the .NET Framework version 2.0.

Binds the value of a session variable to a parameter object.

Namespace: System.Web.UI.WebControls
Assembly: System.Web (in system.web.dll)

public class SessionParameter : Parameter
public class SessionParameter extends Parameter
public class SessionParameter extends Parameter

You can use the SessionParameter class to bind the value of an HttpSessionState variable to a parameter used in a parameterized SQL query, business object method, or filtering expression.

The SessionParameter class provides the SessionField property in addition to those inherited from the Parameter class. The SessionField property identifies the session variable from which the SessionParameter retrieves a value.

NoteNote

Controls that bind data to the parameter might throw an exception if a SessionParameter object is specified but no corresponding session variable is set. Similarly, they might display no data if the session variable is set with a null reference (Nothing in Visual Basic). Set the DefaultValue property to avoid these situations where appropriate.

The following code example demonstrates how to use a SessionParameter object to bind the value of an employee ID stored in a Web browser session to a parameterized SQL query. The SessionParameter object is added to a SqlDataSource control's SelectParameters collection, and used by the parameterized SQL query set in the SelectCommand property to retrieve data and display it in a GridView control. Because the SessionParameter object has a DefaultValue property set, data will still be displayed even if you do not set a session variable named "empid" before you run this example.

<%@ Page language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <P>Show My Orders:</P>

      <asp:SqlDataSource
          id="OdbcDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          ConnectionString="dsn=MyOdbcDsn;"
          SelectCommand="SELECT OrderId, CustomerId, OrderDate
                         FROM Orders
                         WHERE EmployeeID = ?
                         ORDER BY CustomerId ASC;">
          <SelectParameters>
              <asp:SessionParameter
                Name="empid"
                SessionField="empid"
                DefaultValue="5" />
          </SelectParameters>
      </asp:SqlDataSource>

      <p>
      <asp:GridView
          id="GridView1"
          runat="server"
          DataSourceID="OdbcDataSource1" />
      </p>
    </form>
  </body>
</HTML>

<%@ Page language="VJ#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
  <body>
    <form id="Form1" method="post" runat="server">
      <P>Show My Orders:</P>

      <asp:SqlDataSource
          id="OdbcDataSource1"
          runat="server"
          ProviderName="System.Data.Odbc"
          ConnectionString="dsn=MyOdbcDsn;"
          SelectCommand = "SELECT OrderId, CustomerId, OrderDate
            FROM Orders
            WHERE EmployeeID = ?
            ORDER BY CustomerId ASC;">
          <SelectParameters>
              <asp:SessionParameter
                Name="empid"
                SessionField="empid"
                DefaultValue="5" />
          </SelectParameters>
      </asp:SqlDataSource>

      <p>
      <asp:GridView
          id="GridView1"
          runat="server"
          DataSourceID="OdbcDataSource1" />
      </p>
    </form>
  </body>
</HTML>

System.Object
   System.Web.UI.WebControls.Parameter
    System.Web.UI.WebControls.SessionParameter

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft