Exemple #1
0
        protected void Save_Click(object sender, EventArgs e)
        {
            try
            {
                // var all = new List<string[]>();
                var    tenantId = long.Parse(SessionHelper.GetTenantID(HttpContext.Current.Session));
                string fId      = FileID.Value;
                //long preaId = 0;



                if (!string.IsNullOrEmpty(fId))
                {
                    string ext = EXT.Value;
                    //all = ErecruitHelper.GetLinesFromFile(fId, ext);

                    var             data   = File.ReadAllBytes(fId);
                    ImportFromExcel import = new ImportFromExcel();
                    if (ext == ".xls")
                    {
                        import.LoadXls(data);
                    }
                    else if (ext == ".xlsx")
                    {
                        import.LoadXlsx(data);
                    }
                    //first parameter it's the sheet number in the excel workbook
                    //second paramter it's the number of rows to skip at the start(we have an header in the file)
                    List <QuestionUploadModel> all = import.ExcelToList <QuestionUploadModel>(0, 1);

                    if (all.Count() < 1)
                    {
                        resultLbl.Text = "Either you uploaded an empty file or you provided wrong file type. Only allowed file type is .csv, .xls or.xlsx!";
                    }
                    else
                    {
                        resultLbl.Text = "";
                        var Qtype = DropDownList1.SelectedValue;
                        var ex    = 0;
                        var obj   = new List <object>();
                        foreach (var s in all)
                        {
                            ex += _db.SearchQuestion(s.Question.ToLower()).Where(x => x.TypeId == long.Parse(Qtype)).Count();
                            T_Question q = new T_Question
                            {
                                PreambleId = 0,
                                TypeId     = long.Parse(Qtype),
                                OptionType = (long)ErecruitHelper.OptionType.Single,
                                IsActive   = true,
                                Details    = s.Question,
                                TenantId   = tenantId,
                                Section    = s.Section,
                                AddedBy    = SessionHelper.FetchEmail(Session),
                                DateAdded  = DateTime.Now
                            };
                            _db.T_Question.Add(q);
                            _db.SaveChanges();
                            var opts = new List <T_Option>();
                            if (!string.IsNullOrEmpty(s.A))
                            {
                                opts.Add(new T_Option
                                {
                                    Q_Id      = q.Id,
                                    Details   = s.A,
                                    Answer    = s.Answer == ErecruitHelper.OptionIndex.A.ToString(),
                                    AddedBy   = SessionHelper.FetchEmail(Session),
                                    DateAdded = DateTime.Now
                                });
                            }
                            if (!string.IsNullOrEmpty(s.B))
                            {
                                opts.Add(new T_Option
                                {
                                    Q_Id      = q.Id,
                                    Details   = s.B,
                                    Answer    = s.Answer == ErecruitHelper.OptionIndex.B.ToString(),
                                    AddedBy   = SessionHelper.FetchEmail(Session),
                                    DateAdded = DateTime.Now
                                });
                            }
                            if (!string.IsNullOrEmpty(s.C))
                            {
                                opts.Add(new T_Option
                                {
                                    Q_Id      = q.Id,
                                    Details   = s.C,
                                    Answer    = s.Answer == ErecruitHelper.OptionIndex.C.ToString(),
                                    AddedBy   = SessionHelper.FetchEmail(Session),
                                    DateAdded = DateTime.Now
                                });
                            }
                            if (!string.IsNullOrEmpty(s.D))
                            {
                                opts.Add(new T_Option
                                {
                                    Q_Id      = q.Id,
                                    Details   = s.D,
                                    Answer    = s.Answer == ErecruitHelper.OptionIndex.D.ToString(),
                                    AddedBy   = SessionHelper.FetchEmail(Session),
                                    DateAdded = DateTime.Now
                                });
                            }
                            if (!string.IsNullOrEmpty(s.E))
                            {
                                opts.Add(new T_Option
                                {
                                    Q_Id      = q.Id,
                                    Details   = s.E,
                                    Answer    = s.Answer == ErecruitHelper.OptionIndex.E.ToString(),
                                    AddedBy   = SessionHelper.FetchEmail(Session),
                                    DateAdded = DateTime.Now
                                });
                            }
                            // _db.T_Option.InsertAllOnSubmit(opts);
                            ExtensionMethods.InsertAllOnSubmit(_db.T_Option, opts);
                            _db.SaveChanges();
                        }
                        if (ex > 0)
                        {
                            resultLbl.Text = "Some Question(s) already exist in the system therefore were skipped.";
                        }
                        else
                        {
                            resultLbl.Text = "Questions Uploaded";
                        }
                    }
                }
                else
                {
                    HttpPostedFile file = QuestionFile.PostedFile;

                    if (file != null && file.ContentLength > 0)
                    {
                        string fname  = Path.GetFileName(file.FileName);
                        string ext    = System.IO.Path.GetExtension(file.FileName);
                        string fileID = Guid.NewGuid().ToString();
                        string path   = "";
                        // var all =  new List<string[]>();
                        if (Directory.Exists(Server.MapPath("~/UploadedQuestions/")))
                        {
                            path = Server.MapPath(Path.Combine("~/UploadedQuestions/", fileID + ext));
                            // PPath = Path.Combine("~/Passports/", cand.Code + ext);
                            file.SaveAs(path);
                        }
                        else
                        {
                            Directory.CreateDirectory(Server.MapPath("~/UploadedQuestions/"));
                            path = Server.MapPath(Path.Combine("~/UploadedQuestions/", fileID + ext));
                            // PPath = Path.Combine("~/Passports/", cand.Code + ext);
                            file.SaveAs(path);
                        }
                        var             data   = File.ReadAllBytes(path);
                        ImportFromExcel import = new ImportFromExcel();
                        if (ext == ".xls")
                        {
                            import.LoadXls(data);
                        }
                        else if (ext == ".xlsx")
                        {
                            import.LoadXlsx(data);
                        }
                        //first parameter it's the sheet number in the excel workbook
                        //second paramter it's the number of rows to skip at the start(we have an header in the file)
                        List <QuestionUploadModel> all = import.ExcelToList <QuestionUploadModel>(0, 1);

                        if (all.Count() < 1)
                        {
                            resultLbl.Text = "Either you uploaded an empty file or you provided wrong file type. Only allowed file type is .csv, .xls or.xlsx!";
                        }
                        else
                        {
                            resultLbl.Text = "";
                            var Qtype = DropDownList1.SelectedValue;
                            var ex    = 0;
                            var obj   = new List <object>();
                            foreach (var s in all)
                            {
                                ex += _db.SearchQuestion(s.Question.ToLower()).Where(x => x.TypeId == long.Parse(Qtype)).Count();
                                T_Question q = new T_Question
                                {
                                    PreambleId = 0,
                                    TypeId     = long.Parse(Qtype),
                                    OptionType = (long)ErecruitHelper.OptionType.Single,
                                    IsActive   = true,
                                    Details    = s.Question,
                                    Section    = s.Section,
                                    TenantId   = tenantId,
                                    AddedBy    = SessionHelper.FetchEmail(Session),
                                    DateAdded  = DateTime.Now
                                };
                                _db.T_Question.Add(q);
                                _db.SaveChanges();
                                var opts = new List <T_Option>();
                                if (!string.IsNullOrEmpty(s.A))
                                {
                                    opts.Add(new T_Option
                                    {
                                        Q_Id      = q.Id,
                                        Details   = s.A,
                                        Answer    = s.Answer == ErecruitHelper.OptionIndex.A.ToString(),
                                        AddedBy   = SessionHelper.FetchEmail(Session),
                                        DateAdded = DateTime.Now
                                    });
                                }
                                if (!string.IsNullOrEmpty(s.B))
                                {
                                    opts.Add(new T_Option
                                    {
                                        Q_Id      = q.Id,
                                        Details   = s.B,
                                        Answer    = s.Answer == ErecruitHelper.OptionIndex.B.ToString(),
                                        AddedBy   = SessionHelper.FetchEmail(Session),
                                        DateAdded = DateTime.Now
                                    });
                                }
                                if (!string.IsNullOrEmpty(s.C))
                                {
                                    opts.Add(new T_Option
                                    {
                                        Q_Id      = q.Id,
                                        Details   = s.C,
                                        Answer    = s.Answer == ErecruitHelper.OptionIndex.C.ToString(),
                                        AddedBy   = SessionHelper.FetchEmail(Session),
                                        DateAdded = DateTime.Now
                                    });
                                }
                                if (!string.IsNullOrEmpty(s.D))
                                {
                                    opts.Add(new T_Option
                                    {
                                        Q_Id      = q.Id,
                                        Details   = s.D,
                                        Answer    = s.Answer == ErecruitHelper.OptionIndex.D.ToString(),
                                        AddedBy   = SessionHelper.FetchEmail(Session),
                                        DateAdded = DateTime.Now
                                    });
                                }
                                if (!string.IsNullOrEmpty(s.E))
                                {
                                    opts.Add(new T_Option
                                    {
                                        Q_Id      = q.Id,
                                        Details   = s.E,
                                        Answer    = s.Answer == ErecruitHelper.OptionIndex.E.ToString(),
                                        AddedBy   = SessionHelper.FetchEmail(Session),
                                        DateAdded = DateTime.Now
                                    });
                                }
                                ExtensionMethods.InsertAllOnSubmit(_db.T_Option, opts);
                                //_db.T_Option.InsertAllOnSubmit(opts);
                                _db.SaveChanges();
                            }
                            if (ex > 0)
                            {
                                resultLbl.Text = "Some Question(s) already exist in the system therefore were skipped.";
                            }
                            else
                            {
                                resultLbl.Text = "Questions Uploaded";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErecruitHelper.SetErrorData(ex, Session);
                Response.Redirect("ErrorPage.aspx", false);
            }
        }