Ejemplo n.º 1
0
        /// <summary>
        /// Save subscriber to database
        /// </summary>
        /// <param name="listID">list from which subscriber belogs</param>
        /// <param name="userID"></param>
        public void saveSubscriber(string userID)
        {
            ListSusbscriber lSub = new ListSusbscriber();

            if (isSubscriberExist(this.EmailAddress, this.ListID, userID) == false)
            {
                lSub.ListID = this.ListID;
                // this.StatusID = 3;
                this.AddedDate = DateTime.Now;
                using (dbcontext = new ApplicationDbContext())
                {
                    using (var trans = dbcontext.Database.BeginTransaction())
                    {
                        //Subscriber not present in list
                        try
                        {
                            dbcontext.M_Subscribers.Add(this);
                            dbcontext.SaveChanges();
                            lSub.SubscribersID = this.SubscriberID;
                            dbcontext.ListSusbscribers.Add(lSub);
                            dbcontext.SaveChanges();
                            trans.Commit();
                        }
                        catch (SqlException ex)
                        {
                            //failed to save in subscriber or listsubscriber,clear model ans add model error
                            trans.Rollback();
                            obj = new M_CustomException((int)ErorrTypes.SqlExceptions, "Problem in saving data", ex.StackTrace, ErorrTypes.SqlExceptions.ToString(), Utlities.GetURL(), ex.LineNumber);

                            obj.LogException();
                            throw obj;
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            obj = new M_CustomException((int)ErorrTypes.others, "Some problem occured while processing request", ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                            obj.LogException();
                            throw obj;
                        }
                    }
                }
            }
            else
            {
                //Subscriber already present in list,clear model ans add model error
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Imports CSV data and saves to database
        /// </summary>
        /// <param name="UploadFile"></param>
        /// <param name="model"></param>
        public void ImportCSV(HttpPostedFileBase UploadFile, SubscribersViewModel model)
        {
            subscriber = new M_Subscriber();
            string filename = System.IO.Path.GetFileNameWithoutExtension(UploadFile.FileName);

            if (UploadFile.FileName.EndsWith(".csv"))
            {
                using (dbcontext = new ApplicationDbContext())
                {
                    Stream stream   = UploadFile.InputStream;
                    string thisFile = filename + "_" + model.ListID + ".csv";
                    // var path = System.IO.Path.Combine(Server.MapPath("~/CSVFiles"), thisFile);
                    DataTable csvTable = new DataTable();
                    using (CsvReader csvReader = new CsvReader(new StreamReader(stream), true))
                    {
                        csvTable.Load(csvReader);
                    }
                    model.dataTable = csvTable;
                    //read column headers
                    var columnHeaders = (from DataColumn dc in model.dataTable.Columns select dc.ColumnName).ToArray();
                    //save list to database
                    if (model.dataTable.Rows.Count > 0)
                    {
                        //UploadFile.SaveAs(path);
                        for (int i = 0; i < model.dataTable.Rows.Count; i++)
                        {
                            try
                            {
                                List <string> sub = new List <string>();
                                sub = dbcontext.M_Subscribers.Where(l => l.ListID == model.ListID).Select(m => m.EmailAddress).ToList();
                                bool ispresent = false;
                                try
                                {
                                    ispresent = sub.Any(s => s == model.dataTable.Rows[i]["EmailAddress"].ToString());
                                }
                                catch (ArgumentException ex)
                                {
                                    //ModelState.AddModelError("Fileerr", "Please see sample file format");
                                    //return View();
                                }
                                if (ispresent == false)
                                {
                                    ListSusbscriber lSub = new ListSusbscriber();
                                    lSub.ListID = model.ListID;
                                    using (var trans = dbcontext.Database.BeginTransaction())
                                    {
                                        // ObjectParameter objParam = new ObjectParameter("ID", typeof(int));
                                        try
                                        {
                                            subscriber.ListID                = Convert.ToInt32(model.ListID);
                                            subscriber.FirstName             = model.dataTable.Rows[i]["FirstName"].ToString();
                                            subscriber.LastName              = model.dataTable.Rows[i]["LastName"].ToString();
                                            subscriber.EmailAddress          = model.dataTable.Rows[i]["EmailAddress"].ToString();
                                            subscriber.AlternateEmailAddress = model.dataTable.Rows[i]["AlternateEmailAddress"].ToString();
                                            subscriber.Address               = model.dataTable.Rows[i]["Address"].ToString();
                                            subscriber.Country               = model.dataTable.Rows[i]["Country"].ToString();
                                            subscriber.City      = model.dataTable.Rows[i]["City"].ToString();
                                            subscriber.AddedDate = DateTime.Now;
                                            // subscriber.StatusID = 3;
                                            dbcontext.M_Subscribers.Add(subscriber);
                                            dbcontext.SaveChanges();
                                            //dbcontext.ImportSubscribers(Convert.ToInt32(model.ListID), model.dataTable.Rows[i]["FirstName"].ToString(),
                                            //      model.dataTable.Rows[i]["LastName"].ToString(), model.dataTable.Rows[i]["EmailAddress"].ToString(), model.dataTable.Rows[i]["AlternateEmailAddress"].ToString(),
                                            //      model.dataTable.Rows[i]["Address"].ToString(), model.dataTable.Rows[i]["Country"].ToString(), model.dataTable.Rows[i]["City"].ToString(),
                                            //      DateTime.Now.ToString(), objParam);

                                            //lSub.SubscribersID = Convert.ToInt32(objParam.Value);
                                            // lSub.SubscribersID = (int?)((SqlParameter)param[9]).Value;
                                            lSub.SubscribersID = subscriber.SubscriberID;
                                            dbcontext.ListSusbscribers.Add(lSub);
                                            dbcontext.SaveChanges();
                                            trans.Commit();
                                        }
                                        catch (SqlException)
                                        {
                                            trans.Rollback();
                                        }
                                        catch (Exception ex)
                                        {
                                            obj = new M_CustomException((int)ErorrTypes.others, ex.Message, ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                                            obj.LogException();
                                            throw obj;
                                        }
                                    }
                                }
                                else
                                {
                                    //   ModelState.AddModelError("present", "Some subscribers already present");
                                }
                            }
                            catch (ArgumentException ex)
                            {
                                //ModelState.AddModelError("Fileerr", "Please see sample file format");
                                //return View();
                            }
                            catch (Exception ex)
                            {
                                obj = new M_CustomException((int)ErorrTypes.others, ex.Message, ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                                obj.LogException();
                                throw obj;
                            }
                        }
                    }
                }
            }
        }