取Session不能在方法外面取,不然会报上述错误。
楼主需要把取Session写在某个方法里,如果这个UserID需要整个页面都能用,可以把UserID定义到外面,如:
public partial class OrderQuery : System.Web.UI.Page
{
protected SqlConnection con = new SqlConnection(Common.Conn);
private String UserID="";
protected void Page_Load(object sender, EventArgs e)
{
String UserID = Session["UserName"].ToString();
}
把UserID定义到外面