c# - I have a scenario I am unable to understand, regarding the session variables -


note:-the question pertains asp.net , c#.

i have 3 functions

  1. getdatabysearchcriteria.
  2. getdata.
  3. getgriddata.

the first function called when page loaded. second function gets called when user makes change on page , and presses btn (getdata) set session variable holds search criteria data griddata object. third function called second function data griddata object griddata user defined class. later in calling function converted json object used render grid table(jqgrid).

problem:- maintain session in order facilitate temporary search criteria holder use when intended user navigates 1 page in same module. in code set session variable when user presses getdata , according current requirement had put hard code changes change data session particular pages. in doing session data automatically changing. unable figure out why. please help.

here's code :-

[system.web.services.webmethod] public static string getdatabysearchcriteria(int reportid) // called when page loaded {     if (sessionvariables.roleid == 0)     {         return "null";     }     try     {         setproperties(reportid);         if (!checkauthorization(reportid)) // write ! before statement after testing if not written         {             return "null";         }         else         {             #region else part             httpcontext.current.session["reportid"] = reportid;             if (httpcontext.current.session["salesusersession"] != null)             {                 setproperties(reportid);                 griddata gd = new griddata();                 gd = httpcontext.current.session["salesusersession"] griddata;                 if (pagetitle.tolower() == "top referring practices")                 {                     gd.rowarea = "referring practice";                     gd.order = "month13@desc";                     gd.selectedcpt = "";                     gd.selectedlocation = "";                     gd.selectedmodality = "";                     gd.selectedreferringpractice = "";                     gd.selectedphysicians = "";                     //gd.direction = "desc";                 }                 else if (pagetitle.tolower() == "top referring physicians")                 {                     gd.rowarea = "physician";                     gd.order = "month13@desc";                     gd.selectedcpt = "";                     gd.selectedlocation = "";                     gd.selectedmodality = "";                     gd.selectedreferringpractice = "";                     gd.selectedphysicians = "";                     //gd.direction = "desc";                 }                 javascriptserializer serializer = new javascriptserializer();                 return serializer.serialize(getgriddata(isvolume: gd.isvolume, pagenumber: 1, pagesize: gd.pagesize, rowarea: gd.rowarea, cpt: gd.selectedcpt, referringphysician: gd.selectedphysicians, referringpractice: gd.selectedreferringpractice, speciality: gd.selectedspeciality, modalityfilter: gd.selectedmodality, locationfilter: gd.selectedlocation, practice: gd.selectedpractice,order:gd.order,orderby:gd.orderby,direction:gd.direction,orderbysummary:gd.orderbysummary,orderbysummarycol:gd.orderbysummarycol,showsubtotalforallgroup: gd.showsubtotalforallgroup));//,direction:gd.direction,orderbysummary:"",orderbysummarycol:""));             }             else             {                 sqlparameter[] parameters = new sqlparameter[3]; // error line                 parameters[0] = new sqlparameter("@userid", sqldbtype.int);                 parameters[0].value = sessionvariables.userid;                 parameters[1] = new sqlparameter("@roleid", sqldbtype.int);                 parameters[1].value = sessionvariables.roleid;                 parameters[2] = new sqlparameter("@reporttitle", sqldbtype.nvarchar);                 // below code added on 11/05/2015 (dd/mm/yyyy) testing purposes                 //if (pagetitle == "top referring practices" || pagetitle == "top referring physicians")                 //{                  //    //pagetitle = "trending report";                 //}                 // modification ends 11/05/2015                 parameters[2].value = pagetitle;                 griddata griddata = new griddata();                 dataset ds = sqlhelper.executedataset(sqlconnectionstring, commandtype.storedprocedure, "sales_getdatabydefaultsearch", parameters);                  datatable datadt = ds.tables[1];                 if (reportid == 1 && ds.tables.count > 3)                 {                     //holidaylist = ds.tables[3].asenumerable().select(r=>r.field<string>("nonworkingdays")).toarray();                 }                  stringbuilder sbcol = new stringbuilder();                 sbcol.append("[");                 foreach (datacolumn column in datadt.columns)                 {                     sbcol.append("\"").append(getpivotcolumnname(column.columnname)).append("\",");                 }                 sbcol.remove(sbcol.length - 1, 1).append("]");                 stringbuilder sbcolmodel = new stringbuilder();                 sbcolmodel.append("[");                 string[] rowareafields = ds.tables[0].rows[0]["rowarea"].tostring().split(',');                 //string rowarea;                 //rowarea = string.join(",", rowareafields);                  foreach (datacolumn column in datadt.columns)                 {                     //if (rowareafields.contains(column.columnname.trim()) && column.columnname != null)                     if (rowareafields.any(s => column.columnname.trim().contains(s)))                     {                         sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"colstyle\",\"align\":\"left\",\"sortable\":true,\"frozen\":true},");                     }                      //else if (holidaylist.contains(column.columnname.trim()) && column.columnname != null)                     //{                     //    sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"holidaystyle\",\"align\":\"right\",\"width\":\"70px\"},");                     //}                     else if (column.ordinal >= datadt.columns.count - noofsummarycolumns)                     {                         if (posnegsummaryvalues.contains(datadt.columns.count - column.ordinal))                         {                             sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"posnegvalue\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");                         }                         else                         {                             if (column.columnname.contains(" actual"))                             {                                 sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"summarycolumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");                             }                             else                             {                                 sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"summarycolumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"120px\"},");                             }                         }                     }                     else                     {                         sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"align\":\"right\",\"width\":\"70px\"},");                     }                  }                 sbcolmodel.remove(sbcolmodel.length - 1, 1);                 sbcolmodel.append("]");                   javascriptserializer serializer = new javascriptserializer();                 list<dictionary<string, object>> rows = new list<dictionary<string, object>>();                 dictionary<string, object> drow;                 foreach (datarow dr in datadt.rows)                 {                     drow = new dictionary<string, object>();                     foreach (datacolumn col in datadt.columns)                     {                         drow.add(getpivotcolumnname(col.columnname), dr[col]);                     }                     rows.add(drow);                 }                 stringbuilder sbjsonrows = new stringbuilder();                 sbjsonrows.append(serializer.serialize(rows));                  stringbuilder json = new stringbuilder();                 json.append("{").append("\"rows\":").append(sbjsonrows.tostring().trim()).append("}");                 json.tostring();                 string[] groupareasortorderarray;                 string[] summarysortorderarray;                 foreach (datarow dr in ds.tables[0].rows)                 {                     griddata.isreportdefault = dr["isdefault"] == dbnull.value ? false : convert.toboolean(dr["isdefault"]);                     griddata.isallreportdefault = dr["isallreportdefault"] == dbnull.value ? false : convert.toboolean(dr["isallreportdefault"]);                     griddata.isvolume = dr["isvolume"] == dbnull.value ? true : convert.toboolean(dr["isvolume"]);                     griddata.rowarea = dr["rowarea"] == dbnull.value ? "modality" : getrowareaforpivot(convert.tostring(dr["rowarea"]));                     griddata.titlename = dr["titlename"] == dbnull.value ? "" : convert.tostring(dr["titlename"]);                     griddata.selectedpractice = dr["practicefilter"] == dbnull.value ? "" : convert.tostring(dr["practicefilter"]);                     griddata.selectedmodality = dr["modalityfilter"] == dbnull.value ? "" : convert.tostring(dr["modalityfilter"]).replace("'","");                     griddata.selectedlocation = dr["locationfilter"] == dbnull.value ? "" : convert.tostring(dr["locationfilter"]);                     griddata.selectedcpt = dr["cptfilter"] == dbnull.value ? "" : convert.tostring(dr["cptfilter"]).replace("'","");                     griddata.selectedphysicians = dr["referringphysicianfilter"] == dbnull.value ? "" : convert.tostring(dr["referringphysicianfilter"]);                     griddata.selectedreferringpractice = dr["referringpracticefilter"] == dbnull.value ? "" : convert.tostring(dr["referringpracticefilter"]);                     griddata.selectedspeciality = dr["specialityfilter"] == dbnull.value ? "" : convert.tostring(dr["specialityfilter"]);                     griddata.pagesize = dr["pagesize"] == dbnull.value ? 30 : convert.toint32(dr["pagesize"]);                     griddata.scheduledreport = dr["isreportscheduled"] == dbnull.value ? false : convert.toboolean(dr["isreportscheduled"]);                     griddata.daily = dr["daily"] == dbnull.value ? false : convert.toboolean(dr["daily"]);                     griddata.weekly = dr["weekly"] == dbnull.value ? -1 : convert.toint32(dr["weekly"]);                     griddata.monthly = dr["monthly"] == dbnull.value ? 0 : convert.toint32(dr["monthly"]);                     griddata.selectedpracticename = dr["practicename"] == dbnull.value ? "" : convert.tostring(dr["practicename"]);                     griddata.showsubtotalforallgroup = dr["showsubtotalforallgroup"] == dbnull.value ? "false" : convert.tostring(dr["showsubtotalforallgroup"]);                     //remap db column corresponding pivot column here                      groupareasortorderarray = convert.tostring(dr["groupareasortorder"]).split('@');                     summarysortorderarray = convert.tostring(dr["summarysortorder"]).split('@');                     griddata.order = dr["order"] == dbnull.value ? "" : convert.tostring(dr["order"]);                     if (groupareasortorderarray[0] != "")                     {                         griddata.orderby = groupareasortorderarray[0] == null ? "" : mapdbcolumnstopivot(groupareasortorderarray[0], datadt);                         griddata.direction = groupareasortorderarray[1] == null ? "" : mapdbcolumnstopivot(groupareasortorderarray[1], datadt);                     }                     if (summarysortorderarray[0] != "")                     {                         griddata.orderbysummary = summarysortorderarray[0] == null ? "" : mapdbcolumnstopivot(summarysortorderarray[0], datadt);                         griddata.orderbysummarycol = summarysortorderarray[1] == null ? "" : mapdbcolumnstopivot(summarysortorderarray[1], datadt);                     }                     //griddata.orderby = griddata.order == "" ? "" :griddata.order.split('@')[0];                     //griddata.direction = griddata.direction==""?"" :griddata.order.split('@')[1];                     //griddata.date = dr["date"] == dbnull.value ? "" : convert.todatetime(dr["date"]).tostring("yyyy-mm-dd");                 }                  griddata.page = 1;                 griddata.total = convert.toint32(math.ceiling(convert.todouble(ds.tables[2].rows[0][0]) / convert.todouble(ds.tables[0].rows[0]["pagesize"])));                 griddata.records = convert.toint32(ds.tables[2].rows[0][0]);                 griddata.col = sbcol.tostring();                 griddata.colmodel = sbcolmodel.tostring();                 griddata.rows = sbjsonrows.tostring().trim();                 return serializer.serialize(griddata);             }             #endregion             //}         }     }     catch (exception ex)     {         elmah.errorlog.getdefault(httpcontext.current).log(new elmah.error(ex));         throw;     } }  [system.web.services.webmethod] public static string getdata(bool isvolume, int pagenumber, int pagesize, string rowarea, string specialityfilter, string modalityfilter, string locationfilter, string cptfilters, string referringpracticefilter, string referringphysiciansfilter, int reportid, string practicefilter, string order, string orderby, string direction, int searchtitleid, string orderbysummary, string orderbysummarycol, string showsubtotalforallgroup) {      try     {         if (httpcontext.current.session["roleid"] == null || sessionvariables.roleid == 0 || sessionvariables.userid == 0)         {             return "null";         }         else         {             if (practicefilter == null || practicefilter == "")             {                 practicefilter = "1";             }             system.collections.sortedlist systemsearchtitleids = new system.collections.sortedlist();             systemsearchtitleids = (system.collections.sortedlist)httpcontext.current.session["systemsearchtitleids"];             setproperties(reportid);             httpcontext.current.session["reportid"] = reportid;             griddata gd;             string[] orderarray;             if (order.trim() != string.empty)             {                 orderarray = order.split('@');                 orderarray[0] = salescolumnmapper(orderarray[0]);                 order = orderarray[0] + "@" + orderarray[1];             }             gd = getgriddata(isvolume: isvolume, pagenumber: pagenumber, pagesize: pagesize, rowarea: rowarea, modalityfilter: modalityfilter, locationfilter: locationfilter, referringphysician: referringphysiciansfilter, referringpractice: referringpracticefilter, speciality: specialityfilter, cpt: cptfilters, practice: practicefilter, order: order, orderby: orderby, direction: direction, orderbysummary: orderbysummary, orderbysummarycol: orderbysummarycol, showsubtotalforallgroup: showsubtotalforallgroup);             httpcontext.current.session["salesusersession"] = gd;              javascriptserializer serializer = new javascriptserializer();             return serializer.serialize(gd);         }     }     catch (exception ex)     {         elmah.errorlog.getdefault(httpcontext.current).log(new elmah.error(ex));         throw;     } }  private static griddata getgriddata(bool isvolume, int pagenumber, int pagesize, string rowarea, string modalityfilter, string locationfilter, string referringphysician, string referringpractice, string speciality, string cpt, string practice, string order,string orderby, string direction, string orderbysummary, string orderbysummarycol,string showsubtotalforallgroup) {     try     {         string[] rowareafields = rowarea.split(',').select(field => field.trim()).toarray();         dataset ds = getdatabyfiltercriteria(isvolume: isvolume, pagenumber: pagenumber, pagesize: pagesize, rowarea: rowarea, modalityfilter: modalityfilter, locationfilter: locationfilter, referringphysician: referringphysician, referringpractice: referringpractice, speciality: speciality, cpt: cpt, practice: practice, order: order, showsubtotalforallgroup: showsubtotalforallgroup);          datatable datadt = ds.tables[0];          stringbuilder sbcol = new stringbuilder();         sbcol.append("[");         foreach (datacolumn column in datadt.columns)         {             sbcol.append("\"").append(getpivotcolumnname(column.columnname.trim())).append("\",");         }         sbcol.remove(sbcol.length - 1, 1);         sbcol.append("]");          stringbuilder sbcolmodel = new stringbuilder();         sbcolmodel.append("[");          foreach (datacolumn column in datadt.columns)         {              if (rowareafields.any(s => getpivotcolumnname(column.columnname.trim()).contains(s)))             {                 sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"colstyle\",\"align\":\"left\",\"sortable\":true,\"frozen\":true},");             }             else if (column.ordinal >= datadt.columns.count - noofsummarycolumns)             {                  if (posnegsummaryvalues.contains(datadt.columns.count - column.ordinal))                 {                     sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"posnegvalue\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");                 }                 else                 {                     if (column.columnname.contains(" actual"))                     {                         sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"summarycolumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"70px\"},");                     }                     else {                         sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"classes\":\"summarycolumns\",\"align\":\"right\",\"sortable\":true,\"width\":\"120px\"},");                     }                 }             }             else             {                 sbcolmodel.append("{\"name\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"index\":\"").append(getpivotcolumnname(column.columnname.trim())).append("\",\"align\":\"right\",\"width\":\"70px\"},");             }          }         sbcolmodel.remove(sbcolmodel.length - 1, 1);         sbcolmodel.append("]");          javascriptserializer serializer = new javascriptserializer();         list<dictionary<string, object>> rows = new list<dictionary<string, object>>();         dictionary<string, object> drow;         foreach (datarow dr in datadt.rows)         {             drow = new dictionary<string, object>();             foreach (datacolumn col in datadt.columns)             {                 drow.add(getpivotcolumnname(col.columnname), dr[col]);             }             rows.add(drow);         }         stringbuilder sbjsonrows = new stringbuilder();         sbjsonrows.append(serializer.serialize(rows));         stringbuilder json = new stringbuilder();         json.append("{");         json.append("\"rows\":");         json.append(sbjsonrows.tostring().trim());         json.append("}");         json.tostring();          griddata griddata = new griddata();         griddata.selectedpractice = practice; // new statement returns practicefilter -- if grid displays can remove          griddata.selectedlocation = locationfilter;         griddata.selectedmodality = modalityfilter;         griddata.selectedreferringpractice = referringpractice;         griddata.selectedphysicians = referringphysician;         griddata.selectedcpt = cpt;         griddata.selectedspeciality = speciality;         griddata.pagesize = pagesize;         griddata.rowarea = rowarea;         griddata.isvolume = isvolume;         griddata.page = 1;         griddata.total = convert.toint32(math.ceiling(convert.todouble(ds.tables[1].rows[0][0]) / convert.todouble(pagesize)));         griddata.records = convert.toint32(ds.tables[1].rows[0][0]);         griddata.col = sbcol.tostring();         griddata.colmodel = sbcolmodel.tostring();         griddata.rows = sbjsonrows.tostring().trim();         griddata.order = order;         griddata.orderby = orderby;         griddata.direction = direction;         griddata.orderbysummary = orderbysummary;         griddata.orderbysummarycol = orderbysummarycol;         griddata.showsubtotalforallgroup = showsubtotalforallgroup;         //temporary solution getting practice name corresponding practice          //var reader = sqlhelper.executereader(sqlconnectionstring, commandtype.text, "select practicename practice  practiceid="+practice);          sqlconnection con = new sqlconnection(sqlconnectionstring);         sqlcommand cmd = new sqlcommand();         object returnvalue;          cmd.commandtext = "select practicename practice  practiceid="+practice;         cmd.commandtype = commandtype.text;         cmd.connection = con;          con.open();          returnvalue = cmd.executescalar();          con.close();         griddata.selectedpracticename = returnvalue.tostring();          return griddata;     }     catch (exception ex)     {         elmah.errorlog.getdefault(httpcontext.current).log(new elmah.error(ex));         throw;     } } 

when gd = httpcontext.current.session["salesusersession"] griddata;

gd object reference httpcontext.current.session["salesusersession"]

that's why session impacted modifications.

this topic may achieve want :

deep cloning objects


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -