How can I access session in a webmethod?

后端 未结 6 1593
礼貌的吻别
礼貌的吻别 2020-11-29 22:42

Can i use session values inside a WebMethod?

I\'ve tried using System.Web.Services.WebMethod(EnableSession = true) but i can\'t access Session parameter

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 23:30

    You can try like this [WebMethod] public static void MyMethod(string ProductID, string Price, string Quantity, string Total)// Add new parameter Here { db_class Connstring = new db_class(); try {

                DataTable dt = (DataTable)HttpContext.Current.Session["aaa"];
    
                if (dt == null)
                {
                    DataTable dtable = new DataTable();
    
                    dtable.Clear();
                    dtable.Columns.Add("ProductID");// Add new parameter Here
                    dtable.Columns.Add("Price");
                    dtable.Columns.Add("Quantity");
                    dtable.Columns.Add("Total");
                    object[] trow = { ProductID, Price, Quantity, Total };// Add new parameter Here
                    dtable.Rows.Add(trow);
                    HttpContext.Current.Session["aaa"] = dtable;                   
                }
                else
                {
                    object[] trow = { ProductID, Price, Quantity, Total };// Add new parameter Here
                    dt.Rows.Add(trow);
                    HttpContext.Current.Session["aaa"] = dt;
                }
    
    
            }
            catch (Exception)
            {
                throw;
            }
        }
    

提交回复
热议问题