public StreamResponseAction GetDataTable(string id)
        {
            try
            {
                if (UseWidgetCache)
                {
                    string htmlRet;
                    SavedWidget widget = Session[id] as SavedWidget;

                    if (widget != null && !String.IsNullOrEmpty(widget.widgetData))
                    {
                        htmlRet = widget.widgetData;
                        return new StreamResponseAction(htmlRet);
                    }
                }

                long maxContent = (long)ISTAT.WebClient.WidgetComplements.Model.Settings.WebClientSettings.Instance.MaxResultHTML;

                string CodeForStreaming = id;

                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                System.IO.TextWriter textWriter = new System.IO.StreamWriter(memoryStream);

                System.Globalization.CultureInfo cFrom =
                    new System.Globalization.CultureInfo(
                        (((DataObjectForStreaming)Session[CodeForStreaming]).Configuration.DecimalSeparator.ToString().Trim() == ".") ? "EN" : "IT");
                System.Globalization.CultureInfo cTo =
                    new System.Globalization.CultureInfo(
                        (ConfigurationManager.AppSettings["DecimalCulture"].ToString().Trim() == ".") ? "EN" : "IT");

                SessionQuery query = SessionQueryManager.GetSessionQuery(Session);

                DataWidget.StreamDataTable(
                    Session[CodeForStreaming],
                    textWriter,
                    (ConfigurationManager.AppSettings["ParseSDMXAttributes"].ToString().ToLower() == "true"),
                    cFrom, cTo, query);

                textWriter.Flush(); // added this line
                byte[] bytesInStream = memoryStream.ToArray(); // simpler way of converting to array
                memoryStream.Close();

                if (UseWidgetCache)
                {
                    CacheWidget cache = new CacheWidget(connectionStringSetting.ConnectionString);

                    DataObjectForStreaming dOFS = Session[CodeForStreaming] as DataObjectForStreaming;
                    if (dOFS != null && dOFS.WidgetID > 0)
                    {
                        var htmlOutput = System.Text.Encoding.Default.GetString(bytesInStream);
                        cache.InsertWidget(dOFS.WidgetID, htmlOutput, dOFS.Configuration.Locale);
                    }
                }

                if ((bytesInStream.Length / 1000) > maxContent)
                {
                    this.HttpContext.Response.Clear();
                    return new StreamResponseAction("Number of cells too big. Select a less number of cells.");
                }
                else
                {
                    this.HttpContext.Response.Clear();
                    this.HttpContext.Response.ContentType = "text/html";
                    //this.HttpContext.Response.AddHeader("content-disposition", "attachment;    filename=name_you_file.xls");
                    this.HttpContext.Response.BinaryWrite(bytesInStream);
                    this.HttpContext.Response.End();
                }

                return new StreamResponseAction();
            }
            catch (Exception ex)
            {
                return new StreamResponseAction(ex.Message);
            }
        }
        public ActionResult IsCachingDataSet()
        {
            GetDataObject PostDataArrived = CS.GetPostData<GetDataObject>(this.Request);
            CacheWidget cache = new CacheWidget(connectionStringSetting.ConnectionString);

            /*reset query*/
            var query = new SessionQuery { CurrentCulture = System.Threading.Thread.CurrentThread.CurrentCulture };
            if (SessionQueryManager.SessionQueryExistsAndIsValid(Session))
            {
                query = SessionQueryManager.GetSessionQuery(Session);
            }
            query.Reset();

            if (PostDataArrived.WidgetId > 0 && UseWidgetCache)
            {
                if (cache.IsCachedWidget(PostDataArrived.WidgetId, System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName.Trim()))
                    return CS.ReturnForJQuery(JSONConst.Success);
            }

            return CS.ReturnForJQuery(JSONConst.Error);
        }
        public ActionResult GetData()
        {
            try
            {
                // Not remove this linee
                Utils.App_Data_Path = HttpContext.Server.MapPath("~/App_Data/");

                    GetDataObject PostDataArrived = CS.GetPostData<GetDataObject>(this.Request);
                    PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                    SessionImplObject sdmxObj =
                    (Session[SESSION_KEY] != null) ? Session[SESSION_KEY] as SessionImplObject : new SessionImplObject();

                    SessionQuery query = SessionQueryManager.GetSessionQuery(Session);
                    query._endpointSettings = PostDataArrived.Configuration;

                    CodemapWidget codemapWidget = new CodemapWidget(new GetCodemapObject()
                    {
                        Configuration = PostDataArrived.Configuration
                    }, sdmxObj);

                        CacheWidget cache = new CacheWidget(connectionStringSetting.ConnectionString);

                        string CodeForStreaming = Guid.NewGuid().ToString();
                        // +++ Caching +++
                        if (PostDataArrived.WidgetId > 0 && UseWidgetCache)
                        {
                            SavedWidget widget = cache.GetWidget(PostDataArrived.WidgetId, System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);
                            if (widget != null && !String.IsNullOrEmpty(widget.widgetData))
                            {
                                Session[CodeForStreaming] = widget;
                                return CS.ReturnForJQuery("{\"code\":\"" + CodeForStreaming + "\"}");
                            }
                        }

                        var count = codemapWidget.GetCountObservation(query);
                        long maxResultObs = (long)ISTAT.WebClient.WidgetComplements.Model.Settings.WebClientSettings.Instance.MaxResultObs;

                        if (count <= maxResultObs)
                        {
                        DataWidget dataWidget = new DataWidget(
                            PostDataArrived, sdmxObj,
                            (ConfigurationManager.AppSettings["ParseSDMXAttributes"].ToString().ToLower() == "true"));

                        object DataStream = null;
                        SessionImplObject ret = dataWidget.GetData(out DataStream, query);

                        // store current SessionImplObject in session
                        //nuovo fabio
                        if (Session[SESSION_KEY] == null) Session[SESSION_KEY] = ret;
                        else ((SessionImplObject)Session[SESSION_KEY]).MergeObject(ret);
                        //fine nuovo fabio

                        DataObjectForStreaming dOFS = DataStream as DataObjectForStreaming;
                        if (dOFS != null && PostDataArrived.WidgetId > 0)
                            dOFS.WidgetID = PostDataArrived.WidgetId;

                        Session[CodeForStreaming] = DataStream;

                        return CS.ReturnForJQuery("{\"code\":\"" + CodeForStreaming + "\"}");
                    }
                    else
                        {
                            return CS.ReturnForJQuery(Messages.label_out_max_results + "(max:"+ maxResultObs + " - record:" + count + ")");
                        }
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(ex.Message);
            }
        }