/// <summary>
        /// Get subscriber to be edited
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public M_Subscriber Edit(int?sid)
        {
            if (sid != null)
            {
                using (dbcontext = new ApplicationDbContext())
                {
                    try
                    {
                        subscriber = dbcontext.M_Subscribers.Find(sid);
                    }
                    catch (SqlException ex)
                    {
                        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)
                    {
                        obj = new M_CustomException((int)ErorrTypes.others, "Some problem occured while processing request", ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                        obj.LogException();
                        throw obj;
                    }
                }
            }
            return(subscriber);
        }
        /// <summary>
        /// Delete perticular subscriber from database
        /// </summary>
        /// <param name="sid"></param>
        public void Delete(int?sid)
        {
            if (sid != null)
            {
                using (dbcontext = new ApplicationDbContext())
                {
                    var ls = dbcontext.ListSusbscribers.SingleOrDefault(l => l.SubscribersID == sid);
                    subscriber = dbcontext.M_Subscribers.Find(sid);
                    int?lid = subscriber.ListID;
                    if (ls != null)
                    {
                        try
                        {
                            dbcontext.ListSusbscribers.Remove(ls);
                        }
                        catch (SqlException ex)
                        {
                            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)
                        {
                            obj = new M_CustomException((int)ErorrTypes.others, "Some problem occured while processing request", ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                            obj.LogException();
                            throw obj;
                        }
                    }
                    try
                    {
                        dbcontext.M_Subscribers.Remove(subscriber);
                        dbcontext.SaveChanges();
                    }
                    catch (SqlException ex)
                    {
                        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)
                    {
                        obj = new M_CustomException((int)ErorrTypes.others, "Some problem occured while processing request", ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                        obj.LogException();
                        throw obj;
                    }
                }
            }
        }
        public static bool Unsub(string subid)
        {
            using (dbcontext = new ApplicationDbContext())
            {
                subscriber = new M_Subscriber();
                int?id;
                if (subid != null)
                {
                    try
                    {
                        id                     = dbcontext.M_Trackings.Where(t => t.IdentifierSubscriber == subid).Select(t => t.SubsciberId).FirstOrDefault();
                        subscriber             = dbcontext.M_Subscribers.Find(id);
                        subscriber.Unsubscribe = true;
                        dbcontext.M_Subscribers.Attach(subscriber);
                        dbcontext.Entry(subscriber).Property(p => p.Unsubscribe).IsModified = true;
                        dbcontext.SaveChanges();
                        return(true);
                    }
                    catch (SqlException ex)
                    {
                        obj = new M_CustomException((int)ErorrTypes.SqlExceptions, ex.Message, ex.StackTrace, ErorrTypes.SqlExceptions.ToString(), Utlities.GetURL(), ex.LineNumber);

                        obj.LogException();
                        throw obj;
                    }
                    catch (Exception ex)
                    {
                        obj = new M_CustomException((int)ErorrTypes.others, ex.Message, ex.StackTrace, ErorrTypes.others.ToString(), Utlities.GetURL());
                        obj.LogException();
                        throw obj;
                    }
                }
                else
                {
                    return(false);
                }
            }
        }
        /// <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;
                            }
                        }
                    }
                }
            }
        }