Narasiman nar

Narasiman nar

  • NA
  • 64
  • 22.5k

In Dev environment is working but in production is not work

May 24 2018 8:46 PM
The below code working in Dev environemt but in production shows error.
  1. if (filterCriteria == "2" && dataFormat == "3")  
  2. {  
  3. if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/") + "reports"))  
  4. {  
  5. Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/") + "reports");  
  6. }  
  7. var fileName = "FarmerReportsSurveyQuestions" + DateTime.Now.ToString("yyyy_MM_dd__hh_mm_ss") + ".xlsx";  
  8. var outputDir = HttpContext.Current.Server.MapPath("~") + "\\reports\\";  
  9. var file = new FileInfo(outputDir + fileName);  
  10. try  
  11. {  
  12. using (var package = new ExcelPackage(file))  
  13. {  
  14. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("FARMER/FARM REPORTS");  
  15. worksheet.TabColor = Color.Green;  
  16. worksheet.DefaultRowHeight = 12;  
  17. worksheet.HeaderFooter.FirstFooter.LeftAlignedText = string.Format("Generated: {0}", DateTime.Now.ToShortDateString());  
  18. DataTable dtFarmerFarmReports = dal.DAL_GetFarmer_FarmReports_All_ForExcel(originname, hdnSeasonalYear.Value.ToString());  
  19. if (dtFarmerFarmReports.Rows.Count > 0)  
  20. {  
  21. using (var range = worksheet.Cells[2, 1, 2, 11])  
  22. {  
  23. range.Style.Font.Bold = true;  
  24. range.Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  25. range.Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  26. range.Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  27. range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  28. range.Style.Fill.PatternType = ExcelFillStyle.Solid;  
  29. range.Style.Font.Color.SetColor(Color.Black);  
  30. range.Style.Fill.BackgroundColor.SetColor(Color.Green);  
  31. range.AutoFitColumns();  
  32. }  
  33. worksheet.Cells["A1:BZ1"].Merge = true;  
  34. worksheet.Cells["A1:BZ1"].Value = "FARMER/FARM DATA";  
  35. worksheet.Row(1).Height = 35;  
  36. using (var range = worksheet.Cells[1, 1, 1, 11])  
  37. {  
  38. range.Style.Font.Bold = true;  
  39. range.Style.Font.Size = 22;  
  40. range.Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  41. range.Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  42. range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  43. range.Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  44. range.Style.Fill.PatternType = ExcelFillStyle.Solid;  
  45. range.Style.Fill.BackgroundColor.SetColor(Color.LightSeaGreen);  
  46. range.Style.Font.Color.SetColor(Color.Black);  
  47. range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;  
  48. range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;  
  49. range.Style.ShrinkToFit = false;  
  50. }  
  51. worksheet.Cells[2, 1].Value = "FARMER ID";  
  52. worksheet.Cells[2, 2].Value = "NAME";  
  53. worksheet.Cells[2, 3].Value = "SEASON";  
  54. worksheet.Cells[2, 4].Value = "GENDER";  
  55. worksheet.Cells[2, 5].Value = "DATE OF BIRTH";  
  56. worksheet.Cells[2, 6].Value = "BIRTH LOCATION";  
  57. worksheet.Cells[2, 7].Value = "PHOTO";  
  58. worksheet.Cells[2, 8].Value = "FATHER NAME";  
  59. worksheet.Cells[2, 9].Value = "MOTHER NAME";  
  60. worksheet.Cells[2, 10].Value = "PHONE NUMBER";  
  61. worksheet.Cells[2, 11].Value = "OBSERVATION";  
  62. worksheet.Cells[2, 12].Value = "NO. OF FARMS";  
  63. worksheet.Cells[2, 13].Value = "PRODUCTION ESTIMATION";  
  64. worksheet.Cells[2, 14].Value = "REGION";  
  65. worksheet.Cells[2, 15].Value = "DISTRICT";  
  66. worksheet.Cells[2, 16].Value = "SUB DISTRICT";  
  67. worksheet.Cells[2, 17].Value = "VILLAGE";  
  68. worksheet.Cells[2, 18].Value = "SECTION";  
  69. worksheet.Cells[2, 19].Value = "ZONE";  
  70. worksheet.Cells[2, 20].Value = "LAST YEAR COOPERATIVE";  
  71. worksheet.Cells[2, 21].Value = "CURRENT YEAR COOPERATIVE";  
  72. worksheet.Cells[2, 22].Value = "FARMER OTHER DETAILS";  
  73. worksheet.Cells[2, 23].Value = "FARM NO";  
  74. worksheet.Cells[2, 24].Value = "FARM VISIT NO";  
  75. worksheet.Cells[2, 25].Value = "FARM DETAILS";  
  76. worksheet.Cells[2, 26].Value = "FARM PICTURE 1";  
  77. worksheet.Cells[2, 27].Value = "FARM PICTURE 1 - LATITUDE";  
  78. worksheet.Cells[2, 28].Value = "FARM PICTURE 1 - LONGITUDE";  
  79. worksheet.Cells[2, 29].Value = "FARM PICTURE 1 - TIMESTAMP";  
  80. worksheet.Cells[2, 30].Value = "FARM PICTURE 2";  
  81. worksheet.Cells[2, 31].Value = "FARM PICTURE 2 - LATITUDE";  
  82. worksheet.Cells[2, 32].Value = "FARM PICTURE 2- LONGITUDE";  
  83. worksheet.Cells[2, 33].Value = "FARM PICTURE 2 - TIMESTAMP";  
  84. worksheet.Cells[2, 34].Value = "FARM PICTURE 3";  
  85. worksheet.Cells[2, 35].Value = "FARM PICTURE 3 - LATITUDE";  
  86. worksheet.Cells[2, 36].Value = "FARM PICTURE 3 - LONGITUDE";  
  87. worksheet.Cells[2, 37].Value = "FARM PICTURE 3 - TIMESTAMP";  
  88. worksheet.Cells[2, 38].Value = "AREA IN SQ.METER";  
  89. worksheet.Cells[2, 39].Value = "AREA IN HECTARE";  
  90. worksheet.Cells[2, 40].Value = "DECLARED AREA";  
  91. worksheet.Cells[2, 41].Value = "GPX AREA";  
  92. worksheet.Cells[2, 42].Value = "AREA BALANCE";  
  93. worksheet.Cells[2, 43].Value = "FIELD STAFF";  
  94. #region questions  
  95. int columnindex = 43;  
  96. List<string> lst = new List<string>(); // declare "lst" here  
  97. foreach (DataRow row in dtFarmerFarmReports.Rows)  
  98. {  
  99. var dict = JsonConvert.DeserializeObject<Dictionary<stringstring>>(row["farm_detailsdata"].ToString());  
  100. string str = string.Empty;  
  101. lst.AddRange(dict.Keys); // add the keys inside your foreach  
  102. }  
  103. var distinctList = lst.Distinct().ToList(); // perform the Distinct outside the foreach  
  104. List<ExcelHeader> excelheaderlist = new List<ExcelHeader>();  
  105. foreach (var data in distinctList)  
  106. {  
  107. columnindex++;  
  108. worksheet.Cells[2, columnindex].Value = data;  
  109. ExcelHeader excelHeader = new ExcelHeader  
  110. {  
  111. colIndex = columnindex,  
  112. colName = data  
  113. };  
  114. excelheaderlist.Add(excelHeader);  
  115. }  
  116. #endregion questions  
  117. for (int j = 0; j < dtFarmerFarmReports.Rows.Count; j++)  
  118. {  
  119. int j1 = (j + 3);  
  120. farmerid = dtFarmerFarmReports.Rows[j]["farmer_id"].ToString();  
  121. worksheet.Cells[(j1), 1].Value = dtFarmerFarmReports.Rows[j]["farmer_id"].ToString();  
  122. worksheet.Cells[(j1), 2].Value = dtFarmerFarmReports.Rows[j]["farmer_name"].ToString();  
  123. worksheet.Cells[(j1), 3].Value = dtFarmerFarmReports.Rows[j]["farmer_season"].ToString();  
  124. worksheet.Cells[(j1), 4].Value = dtFarmerFarmReports.Rows[j]["gender"].ToString();  
  125. worksheet.Cells[(j1), 5].Value = dtFarmerFarmReports.Rows[j]["dateofbirth"].ToString();  
  126. worksheet.Cells[(j1), 6].Value = dtFarmerFarmReports.Rows[j]["birthlocation"].ToString();  
  127. if (dtFarmerFarmReports.Rows[j]["farmerphoto"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farmerphoto"].ToString() == null || dtFarmerFarmReports.Rows[j]["farmerphoto"].ToString() == "-")  
  128. {  
  129. worksheet.Cells[(j1), 7].Value = "-";  
  130. }  
  131. else  
  132. {  
  133. worksheet.Cells[(j1), 7].Value = "Click to view";  
  134. worksheet.Cells[(j1), 7].Hyperlink = new Uri(dtFarmerFarmReports.Rows[j]["farmerphoto"].ToString());  
  135. }  
  136. worksheet.Cells[(j1), 8].Value = dtFarmerFarmReports.Rows[j]["fathername"].ToString();  
  137. worksheet.Cells[(j1), 9].Value = dtFarmerFarmReports.Rows[j]["mothername"].ToString();  
  138. worksheet.Cells[(j1), 10].Value = dtFarmerFarmReports.Rows[j]["phonenumber"].ToString();  
  139. worksheet.Cells[(j1), 11].Value = dtFarmerFarmReports.Rows[j]["observation"].ToString();  
  140. worksheet.Cells[(j1), 12].Value = dtFarmerFarmReports.Rows[j]["nooffarms"].ToString();  
  141. worksheet.Cells[(j1), 13].Value = dtFarmerFarmReports.Rows[j]["produtionestimation"].ToString();  
  142. worksheet.Cells[(j1), 14].Value = dtFarmerFarmReports.Rows[j]["regionname"].ToString();  
  143. worksheet.Cells[(j1), 15].Value = dtFarmerFarmReports.Rows[j]["districtname"].ToString();  
  144. worksheet.Cells[(j1), 16].Value = dtFarmerFarmReports.Rows[j]["subdistrictname"].ToString();  
  145. worksheet.Cells[(j1), 17].Value = dtFarmerFarmReports.Rows[j]["villagename"].ToString();  
  146. worksheet.Cells[(j1), 18].Value = dtFarmerFarmReports.Rows[j]["sectionname"].ToString();  
  147. worksheet.Cells[(j1), 19].Value = dtFarmerFarmReports.Rows[j]["zonename"].ToString();  
  148. worksheet.Cells[(j1), 20].Value = dtFarmerFarmReports.Rows[j]["lastyearcooperative"].ToString();  
  149. worksheet.Cells[(j1), 21].Value = dtFarmerFarmReports.Rows[j]["currentyearcoop"].ToString();  
  150. worksheet.Cells[(j1), 22].Value = dtFarmerFarmReports.Rows[j]["farmer_details"].ToString();  
  151. worksheet.Cells[(j1), 23].Value = dtFarmerFarmReports.Rows[j]["farm_no"].ToString();  
  152. worksheet.Cells[(j1), 24].Value = dtFarmerFarmReports.Rows[j]["farm_visitno"].ToString();  
  153. worksheet.Cells[(j1), 25].Value = dtFarmerFarmReports.Rows[j]["farm_detailsdata"].ToString();  
  154. if (dtFarmerFarmReports.Rows[j]["farm_picture_one"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_one"].ToString() == "-" || dtFarmerFarmReports.Rows[j]["farm_picture_one"].ToString() == null)  
  155. {  
  156. worksheet.Cells[(j1), 26].Value = "-";  
  157. }  
  158. else  
  159. {  
  160. worksheet.Cells[(j1), 26].Value = "Click to view";  
  161. worksheet.Cells[(j1), 26].Hyperlink = new Uri(dtFarmerFarmReports.Rows[j]["farm_picture_one"].ToString());  
  162. }  
  163. if (dtFarmerFarmReports.Rows[j]["farm_picture_one_latitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_one_latitude"].ToString() == null)  
  164. {  
  165. worksheet.Cells[(j1), 27].Value = "-";  
  166. }  
  167. else  
  168. {  
  169. worksheet.Cells[(j1), 27].Value = dtFarmerFarmReports.Rows[j]["farm_picture_one_latitude"].ToString();  
  170. }  
  171. if (dtFarmerFarmReports.Rows[j]["farm_picture_one_longitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_one_longitude"].ToString() == null)  
  172. {  
  173. worksheet.Cells[(j1), 28].Value = "-";  
  174. }  
  175. else  
  176. {  
  177. worksheet.Cells[(j1), 28].Value = dtFarmerFarmReports.Rows[j]["farm_picture_one_longitude"].ToString();  
  178. }  
  179. if (dtFarmerFarmReports.Rows[j]["farm_picture_one_timestamp"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_one_timestamp"].ToString() == null)  
  180. {  
  181. worksheet.Cells[(j1), 29].Value = "-";  
  182. }  
  183. else  
  184. {  
  185. worksheet.Cells[(j1), 29].Value = dtFarmerFarmReports.Rows[j]["farm_picture_one_timestamp"].ToString();  
  186. }  
  187. if (dtFarmerFarmReports.Rows[j]["farm_picture_two"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_two"].ToString() == "-" || dtFarmerFarmReports.Rows[j]["farm_picture_two"].ToString() == null)  
  188. {  
  189. worksheet.Cells[(j1), 30].Value = "-";  
  190. }  
  191. else  
  192. {  
  193. worksheet.Cells[(j1), 30].Value = "Click to view";  
  194. worksheet.Cells[(j1), 30].Hyperlink = new Uri(dtFarmerFarmReports.Rows[j]["farm_picture_two"].ToString());  
  195. }  
  196. if (dtFarmerFarmReports.Rows[j]["farm_picture_two_latitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_two_latitude"].ToString() == null)  
  197. {  
  198. worksheet.Cells[(j1), 31].Value = "-";  
  199. }  
  200. else  
  201. {  
  202. worksheet.Cells[(j1), 31].Value = dtFarmerFarmReports.Rows[j]["farm_picture_two_latitude"].ToString();  
  203. }  
  204. if (dtFarmerFarmReports.Rows[j]["farm_picture_two_longitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_two_longitude"].ToString() == null)  
  205. {  
  206. worksheet.Cells[(j1), 32].Value = "-";  
  207. }  
  208. else  
  209. {  
  210. worksheet.Cells[(j1), 32].Value = dtFarmerFarmReports.Rows[j]["farm_picture_two_longitude"].ToString();  
  211. }  
  212. if (dtFarmerFarmReports.Rows[j]["farm_picture_two_timestamp"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_two_timestamp"].ToString() == null)  
  213. {  
  214. worksheet.Cells[(j1), 33].Value = "-";  
  215. }  
  216. else  
  217. {  
  218. worksheet.Cells[(j1), 33].Value = dtFarmerFarmReports.Rows[j]["farm_picture_two_timestamp"].ToString();  
  219. }  
  220. if (dtFarmerFarmReports.Rows[j]["farm_picture_three"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_two"].ToString() == "-" || dtFarmerFarmReports.Rows[j]["farm_picture_three"].ToString() == null)  
  221. {  
  222. worksheet.Cells[(j1), 34].Value = "-";  
  223. }  
  224. else  
  225. {  
  226. worksheet.Cells[(j1), 34].Value = "Click to view";  
  227. worksheet.Cells[(j1), 34].Hyperlink = new Uri(dtFarmerFarmReports.Rows[j]["farm_picture_three"].ToString());  
  228. }  
  229. if (dtFarmerFarmReports.Rows[j]["farm_picture_three_latitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_three_latitude"].ToString() == null)  
  230. {  
  231. worksheet.Cells[(j1), 35].Value = "-";  
  232. }  
  233. else  
  234. {  
  235. worksheet.Cells[(j1), 35].Value = dtFarmerFarmReports.Rows[j]["farm_picture_three_latitude"].ToString();  
  236. }  
  237. if (dtFarmerFarmReports.Rows[j]["farm_picture_three_longitude"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_three_longitude"].ToString() == null)  
  238. {  
  239. worksheet.Cells[(j1), 36].Value = "-";  
  240. }  
  241. else  
  242. {  
  243. worksheet.Cells[(j1), 36].Value = dtFarmerFarmReports.Rows[j]["farm_picture_three_longitude"].ToString();  
  244. }  
  245. if (dtFarmerFarmReports.Rows[j]["farm_picture_three_timestamp"].ToString() == "" || dtFarmerFarmReports.Rows[j]["farm_picture_three_timestamp"].ToString() == null)  
  246. {  
  247. worksheet.Cells[(j1), 37].Value = "-";  
  248. }  
  249. else  
  250. {  
  251. worksheet.Cells[(j1), 37].Value = dtFarmerFarmReports.Rows[j]["farm_picture_three_timestamp"].ToString();  
  252. }  
  253. worksheet.Cells[(j1), 38].Value = dtFarmerFarmReports.Rows[j]["farm_areasqmeter"].ToString();  
  254. worksheet.Cells[(j1), 39].Value = dtFarmerFarmReports.Rows[j]["farm_areahectare"].ToString();  
  255. worksheet.Cells[(j1), 40].Value = dtFarmerFarmReports.Rows[j]["farm_declaredarea"].ToString();  
  256. worksheet.Cells[(j1), 41].Value = dtFarmerFarmReports.Rows[j]["farm_gpxarea"].ToString();  
  257. worksheet.Cells[(j1), 42].Value = dtFarmerFarmReports.Rows[j]["farm_area_bal"].ToString();  
  258. worksheet.Cells[(j1), 43].Value = dtFarmerFarmReports.Rows[j]["fieldstaffname"].ToString();  
  259. var dict = JsonConvert.DeserializeObject<Dictionary<stringstring>>(dtFarmerFarmReports.Rows[j]["farm_detailsdata"].ToString());  
  260. foreach (var dicdata in dict)  
  261. {  
  262. int colval = excelheaderlist.Where(x => x.colName == dicdata.Key).Select(y => y.colIndex).FirstOrDefault();  
  263. if (colval > 0)  
  264. { worksheet.Cells[(j1), colval].Value = dicdata.Value; }  
  265. }  
  266. }  
  267. worksheet.Cells["A2:BZ2"].AutoFilter = true;  
  268. }  
  269. else  
  270. {  
  271. worksheet.Cells["A1:I1"].Merge = true;  
  272. worksheet.Cells["A1:I1"].Value = "NO FARMER/FARM DATA";  
  273. worksheet.Row(1).Height = 35;  
  274. using (var range = worksheet.Cells[1, 1, 1, 11])  
  275. {  
  276. range.Style.Font.Bold = true;  
  277. range.Style.Font.Size = 18;  
  278. range.Style.Border.Top.Style = ExcelBorderStyle.Thin;  
  279. range.Style.Border.Right.Style = ExcelBorderStyle.Thin;  
  280. range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;  
  281. range.Style.Border.Left.Style = ExcelBorderStyle.Thin;  
  282. range.Style.Fill.PatternType = ExcelFillStyle.Solid;  
  283. range.Style.Fill.BackgroundColor.SetColor(Color.Red);  
  284. range.Style.Font.Color.SetColor(Color.Black);  
  285. range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;  
  286. range.Style.VerticalAlignment = ExcelVerticalAlignment.Center;  
  287. range.Style.ShrinkToFit = false;  
  288. }  
  289. }  
  290. worksheet.Cells.AutoFitColumns();  
  291. package.Workbook.Properties.Title = "Farmer Reports";  
  292. package.Workbook.Properties.Author = "Olam";  
  293. package.Workbook.Properties.Company = "Olam";  
  294. package.Save();  
  295. modalPopupExport.Hide();  
  296. }  
  297. Response.Clear();  
  298. Response.ContentType = ContentType;  
  299. Response.AppendHeader("Content-Disposition""attachment; filename=" + file.Name);  
  300. Response.WriteFile(outputDir + fileName);  
  301. HttpContext.Current.ApplicationInstance.CompleteRequest();  
  302. Response.End();  
  303. }  
  304. catch (Exception ex)  
  305. {  
  306. BindDetails();  
  307. this.modalPopupExport.Hide();  
  308. showStatusTrue.Style.Add("display""none");  
  309. showStatusTrue.InnerHtml = "";  
  310. showStatusWarning.Style.Add("display""none");  
  311. showStatusWarning.InnerHtml = "";  
  312. showStatusAlready.Style.Add("display""none");  
  313. showStatusAlready.InnerHtml = "";  
  314. showStatusFalse.Style.Add("display""block");  
  315. showStatusFalse.InnerHtml = "Something went wrong while export";  
  316. farmerid = "" + farmerid;  
  317. ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp""<script type='text/javascript'>myFunction();</script>"false);  
  318. Logger log = new Logger();  
  319. log.WriteToErrorLog("FARMER REPORTS EXCEL""EX", ex.Message.ToString(), """");  
  320. }  
  321. }  
  322. private class ExcelHeader  
  323. {  
  324. public int colIndex { getset; }  
  325. public string colName { getset; }  
  326. }  
The above code working in Developement environment and build and publish the code in dev environment but in production environment shows error some thing went wrong while export.
 
how to solve this error. please let me know. I am trying to find out for last one day I cannot able to solve this error. please help me how to fix this error.

Answers (5)