public void ProcessRequest(HttpContext context) { var response = System.Web.HttpContext.Current.Response; if (HttpContext.Current.Session["AdminUserID"] != null) { var adata = new AdminData(); var dtbl = adata.GetActiveUsersData(); StringBuilder sb = new StringBuilder(); if (dtbl.Rows.Count > 0) { //headers foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dc.ColumnName.ToString()) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); foreach (DataRow dr in dtbl.Rows) { foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dr[dc.ColumnName].ToString()) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); } response.ClearContent(); response.Clear(); response.ContentType = "text/csv"; response.AddHeader("content-disposition", "attachment; filename= MyPPCPal_ActiveUsers.csv"); response.BufferOutput = true; response.Cache.SetCacheability(HttpCacheability.NoCache); response.Write(sb.ToString()); response.Flush(); response.End(); } else { response.Write("Active users were not found"); } } else { response.Redirect("admin/account/login"); } }
private void KeywordSearchTermReportGenerator(DataSet dataset, out StringBuilder sb) { sb = new StringBuilder(); var dtbl = dataset.Tables[0]; var dtTotal = dataset.Tables[1]; //headers foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dc.ColumnName.ToString().Replace('_', ' ')) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); // var flag = false; var i = 1; foreach (DataRow dr in dtbl.Rows) { var nextKey = ""; var nextQuery = ""; var CurrentKey = dr["Keyword"].ToString().ToLower().Replace(" ", "_"); var currentQuery = dr["Search_Term"].ToString().ToLower().Replace(" ", "_"); if (i < dtbl.Rows.Count) { nextKey = dtbl.Rows[i]["Keyword"].ToString().ToLower().Replace(" ", "_"); nextQuery = dtbl.Rows[i]["Search_Term"].ToString().ToLower().Replace(" ", "_"); } if (CurrentKey == nextKey && currentQuery == nextQuery) { foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dr[dc.ColumnName].ToString()) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); // flag = true; i++; continue; } else if (nextKey != "" && nextQuery != "") { try { foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dr[dc.ColumnName].ToString()) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); // if (flag) // { var CurrentRow = (from row in dtTotal.AsEnumerable() where row.Field <string>("Keyword").ToLower().Replace(" ", "_") == CurrentKey && row.Field <string>("Search_Term").ToLower().Replace(" ", "_") == currentQuery select row).FirstOrDefault(); foreach (DataColumn dc in dtbl.Columns) { var columnName = dc.ColumnName == "Campaign_Name" ? "Total" : null; if (dtTotal.Columns.IndexOf(dc.ColumnName) < 0) { sb.Append(MppUtility.FormatCSV(columnName) + ","); } else { sb.Append(MppUtility.FormatCSV(CurrentRow[dc.ColumnName].ToString()) + ","); } } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); // flag = false; // } i++; } catch (Exception ex) { } } else { foreach (DataColumn dc in dtbl.Columns) { sb.Append(MppUtility.FormatCSV(dr[dc.ColumnName].ToString()) + ","); } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); //if (flag) // { var CurrentRow = (from row in dtTotal.AsEnumerable() where row.Field <string>("Keyword").ToLower().Replace(" ", "_") == CurrentKey && row.Field <string>("Search_Term").ToLower().Replace(" ", "_") == currentQuery select row).FirstOrDefault(); foreach (DataColumn dc in dtbl.Columns) { var columnName = dc.ColumnName == "Campaign_Name" ? "Total" : null; if (dtTotal.Columns.IndexOf(dc.ColumnName) < 0) { sb.Append(MppUtility.FormatCSV(columnName) + ","); } else { sb.Append(MppUtility.FormatCSV(CurrentRow[dc.ColumnName].ToString()) + ","); } } sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); // } // flag = false; } } }