public List<ArticleModel> GetArticles(string strQuery)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataSet DSQuestions = new DataSet();
            DSQuestions = connManager.GetData(strQuery);
            connManager.DisposeConn();

            List<ArticleModel> articles = new List<ArticleModel>();
            ArticleModel article;
            foreach (DataRow row in DSQuestions.Tables[0].Rows)
            {
                article = new ArticleModel();
                article.ArticleID = row["ArticleID"].ToString();
                article.ArticleTitle = row["ArticleTitle"].ToString();
                article.InsertedDate = row["InsertedDate"].ToString();
                article.ThumbsUp = row["ThumbsUp"].ToString();
                article.ThumbsDown = row["ThumbsDown"].ToString();
                article.Views = row["Views"].ToString();
                articles.Add(article);
            }
            return articles;
        }
        public ActionResult LogOut()
        {
            Session["User"] = null;
            Session["Facebook"] = null;
            Session.RemoveAll();
            List<ArticleModel> articles = new List<ArticleModel>();
            ConnManager connManager = new ConnManager();
            articles = connManager.GetArticles("Select * from VwArticles where articleid not in (10044,10045,10046,10047,10048,10049) order by articleId desc");
            connManager.DisposeConn();

            PagingInfo info = new PagingInfo();
            info.SortField = " ";
            info.SortDirection = " ";
            info.PageSize = 10;
            info.PageCount = Convert.ToInt32(Math.Ceiling((double)(articles.Count / info.PageSize)));
            info.CurrentPageIndex = 0;
            var query = articles.OrderBy(c => c.ArticleID).Take(info.PageSize);
            ViewBag.PagingInfo = info;
            ViewBag.UserEmail = null;

            Response.Redirect("../Articles/Index");

            return View("../Articles/Index", articles);
        }
        private void ProcessVotes(string LikeType, string articleID)
        {
            List<string> lstReplies = (List<string>)Session["Articles"];
            string strQuery = "";
            int votes = 0;
            if (lstReplies == null)
            {
                lstReplies = new List<string>();
            }

            if (!lstReplies.Contains(articleID))
            {

                ConnManager connManager = new ConnManager();
                connManager.OpenConnection();

                DataTable dsVotes = connManager.GetDataTable("Select ThumbsUp, ThumbsDown from CodeArticles where ArticleId = " + articleID);

                if (dsVotes != null && dsVotes.Rows.Count > 0)
                {
                    if (LikeType.Equals("Up"))
                    {
                        if (string.IsNullOrEmpty(dsVotes.Rows[0]["ThumbsUp"].ToString()))
                            votes = votes + 1;
                        else
                            votes = int.Parse(dsVotes.Rows[0]["ThumbsUp"].ToString()) + 1;

                        strQuery = "Update CodeArticles set ThumbsUp = " + votes + " where ArticleId = " + articleID;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(dsVotes.Rows[0]["ThumbsDown"].ToString()))
                            votes = votes - 1;
                        else
                            votes = int.Parse(dsVotes.Rows[0]["ThumbsDown"].ToString()) + 1;

                        strQuery = "Update CodeArticles set ThumbsDown = " + votes + " where ArticleId = " + articleID;
                    }
                }

                SqlCommand command = new SqlCommand(strQuery, connManager.DataCon);

                command.ExecuteNonQuery();
                connManager.DisposeConn();

                lstReplies.Add(articleID);
                Session["Articles"] = lstReplies;

            }

            //GetArticleData(articleID.ToString());
        }
        private void GetArticleData(string strArticleId, ref VwArticlesModel model)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataTable dsQuestion = connManager.GetArticle(strArticleId);

            connManager.DisposeConn();
            if (dsQuestion != null)
            {
                if (dsQuestion.Rows.Count > 0)
                {
                    model.ArticleID = long.Parse(dsQuestion.Rows[0]["ArticleId"].ToString());

                    if (!dsQuestion.Rows[0]["EMail"].ToString().Contains("codeanalyze.com"))
                    {
                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["FirstName"].ToString()))
                        {
                            model.AskedUser = "******" + dsQuestion.Rows[0]["FirstName"].ToString() + "<b>";
                            if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["LastName"].ToString()))
                            {
                                model.AskedUser = model.AskedUser + " <b>" + dsQuestion.Rows[0]["LastName"].ToString() + "<b>";
                            }
                        }

                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["ImageURL"].ToString()))
                        {
                            model.ImageURL = dsQuestion.Rows[0]["ImageURL"].ToString();
                        }
                        else
                        {
                            model.ImageURL = "~/Images/Person.JPG";
                        }

                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["Details"].ToString()))
                        {
                            model.AskedUserDetails = dsQuestion.Rows[0]["Details"].ToString();
                        }

                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["Stars"].ToString()))
                        {
                            model.Stars = dsQuestion.Rows[0]["Stars"].ToString();
                        }
                        else
                        {
                            model.Stars = "0";
                        }

                    }

                    Session["AskedUserEMail"] = dsQuestion.Rows[0]["EMail"].ToString();
                    //model.ArticleTitle = "<b>" + dsQuestion.Rows[0]["ArticleTitle"].ToString() + "<b>";
                    model.ArticleTitle = dsQuestion.Rows[0]["ArticleTitle"].ToString();
                    model.ArticleViews = dsQuestion.Rows[0]["Views"].ToString();

                    string filepath = string.Empty;
                    if (Request.Url.ToString().Contains("localhost"))
                          filepath = Server.MapPath("/CodeAnalyzeMVC2015/Articles/" + dsQuestion.Rows[0]["WordFile"].ToString()).Replace("\\Details\\Articles", "");
                    else
                         filepath =  Server.MapPath("/Articles/" + dsQuestion.Rows[0]["WordFile"].ToString()).Replace("\\Details\\Articles", "");

                    using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                    {
                        using (TextReader tr = new StreamReader(fs))
                        {
                            string content = tr.ReadToEnd();
                            model.ArticleDetails = content;
                        }
                    }

                    //if (dsQuestion.Rows[0]["SourceFile"] != DBNull.Value && !string.IsNullOrEmpty(dsQuestion.Rows[0]["SourceFile"].ToString()))
                    //{
                    //    hfSourceFile.Value = dsQuestion.Rows[0]["SourceFile"].ToString();
                    //    lnkBtnSourceCode.Enabled = true;
                    //    lnkBtnSourceCode.ForeColor = Color.Blue;
                    //    lnkBtnSourceCode.Text = "Download Source Code";
                    //}
                    //else
                    //{
                    //    lnkBtnSourceCode.Enabled = false;
                    //    lnkBtnSourceCode.ForeColor = Color.Gray;
                    //    lnkBtnSourceCode.Text = "No Source Code Uploaded";
                    //}

                    if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["YouTubURL"].ToString()))
                    {
                        model.iframeVideoURL = "//www.youtube.com/embed?listType=playlist&list=PLr5xM_46LGUtyEyyGilUu3YH5FTNo7SMH";
                    }

                    model.ThumbsUp = dsQuestion.Rows[0]["ThumbsUp"].ToString();
                    model.ThumbsDown = dsQuestion.Rows[0]["ThumbsDown"].ToString();
                    model.HasDemo = Convert.ToInt32(dsQuestion.Rows[0]["HasDemo"].ToString());

                    if (dsQuestion.Rows[0]["DemoURL"]!=null)
                    {
                        model.DemoURL = dsQuestion.Rows[0]["DemoURL"].ToString();
                    }
                }
            }
        }
        private void BindComments(string strQuery, ref VwArticlesModel model)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataTable dsSolution = connManager.GetDataTable(strQuery);
            string strReplyId = "";
            string tblReplies = "<table width=\"100%\" style=\"word-wrap:normal; word-break:break-all\" cell-padding=\"0\" cell-spacing=\"0\">";

            if (dsSolution != null)
            {
                for (int i = 0; i < dsSolution.Rows.Count; i++)
                {

                    //Response no user details
                    string htrResponseNoByDetailsOuterRow = "<tr>";
                    string htcResponseNoByDetailsOuterCell = "<td style=\"background-color:lightgrey;border-radius:10px;\">";

                    string htmlTblResponseNoByDetails = "<table style=\"width:100%\">";

                    #region table
                    string htmlRowResponseNoByDetails = "<tr>";

                    string htcUserImage = "<td>";
                    if (!string.IsNullOrEmpty(dsSolution.Rows[i]["ImageURL"].ToString()))
                    {
                        if (Request.Url.ToString().Contains("localhost"))
                            htcUserImage += "<img src=\"/CodeAnalyzeMVC2015/" + dsSolution.Rows[i]["ImageURL"].ToString().Replace("~", "") + "\" style=\"height:30px;width:30px\" />";
                        else
                            htcUserImage += "<img src=\"" + dsSolution.Rows[i]["ImageURL"].ToString().Replace("~", "") + "\" style=\"height:30px;width:30px\" />";
                    }
                    else
                    {
                        if (Request.Url.ToString().Contains("localhost"))
                            htcUserImage += "<img src=\"/CodeAnalyzeMVC2015/Images/Person.JPG\" style=\"height:25px;width:25px\" />";
                        else
                            htcUserImage += "<img src=\"/Images/Person.JPG\" style=\"height:25px;width:25px\" />";
                    }
                    htcUserImage += "</td>";

                    #region responseNoBy
                    string htcResponseNoByDetails = "<td valign=\"middle\">";
                    string strFirstName = "";
                    string strAnswers = "";
                    string strRepliedDate = "";

                    string strUserId = dsSolution.Rows[i]["UserId"].ToString();
                    if (!string.IsNullOrEmpty(dsSolution.Rows[i]["FirstName"].ToString()))
                        strFirstName = dsSolution.Rows[i]["FirstName"].ToString();
                    else
                        strFirstName = dsSolution.Rows[i]["EMail"].ToString().Split('@')[0];
                    strRepliedDate = dsSolution.Rows[i]["InsertedDate"].ToString().Split('@')[0];

                    DataTable dsCount = new DataTable();
                    dsCount = connManager.GetDataTable("SELECT COUNT(*) FROM VwArticleReplies WHERE (ArticleId = " + strUserId + ") ");

                    if (dsCount != null && dsCount.Rows.Count != 0)
                    {
                        if (dsCount.Rows[0][0].ToString() != "0")
                        {
                            if (dsCount != null)
                                strAnswers = dsCount.Rows[0][0].ToString();
                            else
                                strAnswers = "none";

                            if (!dsSolution.Rows[i]["EMail"].ToString().Contains("codeanalyze.com"))
                                //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + "</b>  " + strRepliedDate + "";   // Total replies by user: "******".";
                            htcResponseNoByDetails += "<b>" + strFirstName + "</b>  " + strRepliedDate + "";   // Total replies by user: "******".";
                            else
                            {
                                if (!strFirstName.ToLower().Equals("admin"))
                                    //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + "</b>  " + strRepliedDate + "";
                                htcResponseNoByDetails += "<b>" + strFirstName + "</b>  " + strRepliedDate + "";
                                else
                                    //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> " + strRepliedDate + "";
                                htcResponseNoByDetails += "</b> " + strRepliedDate + "";

                            }
                        }
                        else
                            if (!strFirstName.ToLower().Equals("admin"))
                            //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + "</b>  " + strRepliedDate + "";
                        htcResponseNoByDetails += "<b>" + strFirstName + "</b>  " + strRepliedDate + "";

                        else
                            //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> " + strRepliedDate + "";
                        htcResponseNoByDetails += "</b> " + strRepliedDate + "";
                    }
                    else
                        if (!strFirstName.ToLower().Equals("admin"))
                        //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + "</b>  " + strRepliedDate + "";
                    htcResponseNoByDetails += "<b>" + strFirstName + "</b>  " + strRepliedDate + "";
                    else
                        //htcResponseNoByDetails += "Comment No <b>" + (i + 1).ToString() + "</b> " + strRepliedDate + " ";
                    htcResponseNoByDetails += "</b> " + strRepliedDate + " ";

                    htcResponseNoByDetails += "</td>";

                    htmlRowResponseNoByDetails += htcUserImage;
                    htmlRowResponseNoByDetails += htcResponseNoByDetails;
                    #endregion

                    htmlRowResponseNoByDetails += "</tr>";
                    #endregion

                    htmlTblResponseNoByDetails += htmlRowResponseNoByDetails + "</table>";

                    htcResponseNoByDetailsOuterCell += htmlTblResponseNoByDetails + "</td>";
                    htrResponseNoByDetailsOuterRow += htcResponseNoByDetailsOuterCell + "</tr>";

                    string htmlRowSolutionContent = string.Empty;
                    string htcReplyContent = string.Empty;
                    strReplyId = dsSolution.Rows[i]["ReplyId"].ToString();
                    htcReplyContent += "<td>" + dsSolution.Rows[i]["ReplyText"].ToString() + "</td>";
                    htmlRowSolutionContent += "<tr>" + htcReplyContent + "</tr>";

                    tblReplies += htrResponseNoByDetailsOuterRow +  htmlRowSolutionContent;

                    tblReplies += "<tr><td><br /></td></tr>";

                }
            }

            tblReplies += "</table>";
            model.ArticleReplies = tblReplies;
            connManager.DisposeConn();
        }
        private void GetQuestionData(string strQuestionId, ref VwSolutionsModel model)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataTable dsQuestion = connManager.GetQuestion(strQuestionId);
            connManager.DisposeConn();
            long quesID;

            string strQuestionDetails = String.Empty;

            if (dsQuestion != null)
            {
                if (dsQuestion.Rows.Count > 0)
                {
                    quesID = long.Parse(dsQuestion.Rows[0]["QuestionId"].ToString());
                    model.QuestionID = quesID.ToString();
                    if (!dsQuestion.Rows[0]["EMail"].ToString().Contains("codeanalyze.com"))
                    {
                        model.AskedUser = dsQuestion.Rows[0]["FirstName"].ToString() + " ";
                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["LastName"].ToString()))
                        {
                            model.AskedUser = model.AskedUser + "" + dsQuestion.Rows[0]["LastName"].ToString() + "";
                        }

                        if (!string.IsNullOrEmpty(dsQuestion.Rows[0]["ImageURL"].ToString()))
                            model.ImageURL = dsQuestion.Rows[0]["ImageURL"].ToString();
                        else
                            model.ImageURL = "~/Images/Person.JPG";
                    }
                    //else
                    //{
                    //    model.ImageURL = "~/Images/Person.JPG";
                    //}

                    Session["AskedUserEMail"] = dsQuestion.Rows[0]["EMail"].ToString();
                    model.QuestionTitle = dsQuestion.Rows[0]["QuestionTitle"].ToString();

                    strQuestionDetails = dsQuestion.Rows[0]["Question"].ToString().Replace("font-size: x-small", "font-size: medium");
                    strQuestionDetails = StringClean(strQuestionDetails);

                    model.QuestionDetails = "<table style=\"width:100%\"><tr><td>" + strQuestionDetails + "</td></tr></table>";
                    ViewBag.QuestionDetails = model.QuestionDetails;

                    model.QuestionViews = "<b>" + dsQuestion.Rows[0]["Views"].ToString() + "<b>";

                }
            }
        }
        private void ProcessVotes(string LikeType, string ReplyId, string quesID)
        {
            List<string> lstReplies = (List<string>)Session["Replies"];
            string strQuery = "";
            int votes = 0;
            if (lstReplies == null)
            {
                lstReplies = new List<string>();
            }

            if (!lstReplies.Contains(ReplyId))
            {

                ConnManager connManager = new ConnManager();
                connManager.OpenConnection();

                DataTable dsVotes = connManager.GetDataTable("Select ThumbsUp, ThumbsDown from Replies where ReplyId = " + ReplyId);

                if (dsVotes != null && dsVotes.Rows.Count > 0)
                {
                    if (LikeType.Equals("UP"))
                    {
                        if (string.IsNullOrEmpty(dsVotes.Rows[0]["ThumbsUp"].ToString()))
                            votes = votes + 1;
                        else
                            votes = int.Parse(dsVotes.Rows[0]["ThumbsUp"].ToString()) + 1;

                        strQuery = "Update Replies set ThumbsUp = " + votes + " where ReplyId = " + ReplyId;
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(dsVotes.Rows[0]["ThumbsDown"].ToString()))
                            votes = votes - 1;
                        else
                            votes = int.Parse(dsVotes.Rows[0]["ThumbsDown"].ToString()) + 1;

                        strQuery = "Update Replies set ThumbsDown = " + votes + " where ReplyId = " + ReplyId;
                    }
                }

                SqlCommand command = new SqlCommand(strQuery, connManager.DataCon);
                command.CommandText = strQuery;
                command.ExecuteNonQuery();
                connManager.DisposeConn();

                lstReplies.Add(ReplyId);
                Session["Replies"] = lstReplies;
            }
            //BindQuestionAskedUserData("Select * from VwQuestions where QuestionId = " + quesID.ToString() + "");
        }
        private void BindSolution(string strQuery, ref VwSolutionsModel model)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataTable dsSolution = connManager.GetDataTable(strQuery);
            string quesID = RouteData.Values["id"].ToString();
            string strReplyId = "";
            string lblUp, lblDown = "0";
            string tblReplies = "<table style=\"word-wrap:break-word; width:98%; \">";
            string strDeleteRow = string.Empty;
            string strTitle = string.Empty;

            if (RouteData.Values["Title"]!=null )
            {
                strTitle = RouteData.Values["Title"].ToString();
            }
            else
            {
                strTitle = model.QuestionTitle;
            }

            if (dsSolution != null && dsSolution.Rows.Count > 0)
            {
                for (int i = 0; i < dsSolution.Rows.Count; i++)
                {

                    lblUp = dsSolution.Rows[i]["ThumbsUp"].ToString();
                    lblDown = dsSolution.Rows[i]["ThumbsDown"].ToString();

                    strReplyId = dsSolution.Rows[i]["ReplyID"].ToString();
                    //Response no user details
                    string htrResponseNoByDetailsOuterRow = "<tr>";
                    string htcResponseNoByDetailsOuterCell = "<td style=\"background-color:lightgrey; border-radius:10px;\">";

                    #region table
                    string htmlTblResponseNoByDetails = "<table style=\"width:100%\">";

                    string htmlRowResponseNoByDetails = "<tr style=\"width:100%;\">";

                    string htcUserImage = "<td align=\"right\"> ";
                    if (!string.IsNullOrEmpty(dsSolution.Rows[i]["ImageURL"].ToString()))
                    {
                        if (Request.Url.ToString().Contains("localhost"))
                            htcUserImage += "<img title=\"User Avatar\" src=\"/CodeAnalyzeMVC2015" + dsSolution.Rows[i]["ImageURL"].ToString().Replace("~", "") + "\" style=\"height:30px;width:30px\" />";
                        else
                            htcUserImage += "<img  title=\"User Avatar\" src=\"" + dsSolution.Rows[i]["ImageURL"].ToString().Replace("~", "").Replace("/CodeAnalyzeMVC2015", "") + "\" style=\"height:30px;width:30px\" />";
                    }
                    else
                    {
                        if (Request.Url.ToString().Contains("localhost"))
                            htcUserImage += "<img title=\"User Avatar\" src=\"/CodeAnalyzeMVC2015/Images/Person.JPG\" style=\"height:25px;width:25px\" />";
                        else
                            htcUserImage += "<img title=\"User Avatar\" src=\"/Images/Person.JPG\" style=\"height:25px;width:25px\" />";
                    }
                    htcUserImage += "</td>";

                    string htcResponseNoByDetails = "<td valign=\"middle\">";

                    #region responseNoBy
                    string strFirstName = "";
                    string strLastName = "";
                    string strAnswers = "";
                    string strRepliedDate = "";

                    string strUserId = dsSolution.Rows[i]["UserId"].ToString();
                    if (!string.IsNullOrEmpty(dsSolution.Rows[i]["FirstName"].ToString()))
                    {
                        strFirstName = dsSolution.Rows[i]["FirstName"].ToString();
                        strLastName = dsSolution.Rows[i]["LastName"].ToString();
                    }
                    else
                        strFirstName = dsSolution.Rows[i]["EMail"].ToString().Split('@')[0];
                    strRepliedDate = dsSolution.Rows[i]["RepliedDate"].ToString().Split('@')[0];

                    strRepliedDate = DateTime.Parse(strRepliedDate).ToShortDateString();

                    DataTable dsCount = new DataTable();
                    dsCount = connManager.GetDataTable("SELECT COUNT(*) FROM VwSolutions WHERE (RepliedUser = "******") AND (AskedUser <> " + strUserId + ")");

                    if (dsCount != null && dsCount.Rows.Count > 0)
                    {
                        if (dsCount.Rows[0][0].ToString() != "0")
                        {
                            if (dsCount != null && dsCount.Rows.Count > 0)
                                strAnswers = dsCount.Rows[0][0].ToString();
                            else
                                strAnswers = "<b>none</b>";

                            if (!dsSolution.Rows[i]["EMail"].ToString().Contains("codeanalyze.com"))
                                //htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + " " + strLastName + "</b>  ";// + strRepliedDate + "";   // Total replies by user: "******".";
                            htcResponseNoByDetails += "<b>" + strFirstName + " " + strLastName + "</b>  " + strRepliedDate + "";   // Total replies by user: "******".";

                            else
                                //htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + "</b>  ";// + strRepliedDate + "";
                            htcResponseNoByDetails += "<b>" + strFirstName + "</b>  " + strRepliedDate + "";
                            //htc4.InnerHtml = "Comment No <b>" + (i + 1).ToString();
                        }
                        else
                            if (!strFirstName.ToLower().Equals("admin"))
                        //    htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + " " + strLastName + "</b>  ";// + strRepliedDate + "";
                        htcResponseNoByDetails += "<b>" + strFirstName + " " + strLastName + "</b>  " + strRepliedDate + "";
                        else
                            //htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> " + strRepliedDate + "";
                        htcResponseNoByDetails += " " + strRepliedDate;
                    }
                    else
                        if (!strFirstName.ToLower().Equals("admin"))
                        //htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> by <b>" + strFirstName + " " + strLastName + "</b>  ";// + strRepliedDate + "";
                    htcResponseNoByDetails += "<b>" + strFirstName + " " + strLastName + "</b>  " + strRepliedDate + "";

                    else
                        //htcResponseNoByDetails += "Response No <b>" + (i + 1).ToString() + "</b> " + strRepliedDate + " ";
                    htcResponseNoByDetails += " " + strRepliedDate + " ";
                    #endregion
                    htcResponseNoByDetails += "</td>";

                    htmlRowResponseNoByDetails += htcUserImage;
                    htmlRowResponseNoByDetails += htcResponseNoByDetails;
                    htmlRowResponseNoByDetails += AddThumbsUpDown(i, quesID, strReplyId, lblUp, lblDown);

                    htmlRowResponseNoByDetails += "</tr>";
                    htmlTblResponseNoByDetails += htmlRowResponseNoByDetails + "</table>";
                    #endregion

                    htcResponseNoByDetailsOuterCell += htmlTblResponseNoByDetails + "</td>";
                    htrResponseNoByDetailsOuterRow += htcResponseNoByDetailsOuterCell + "</tr>";

                    //Solution Row
                    string htmlRowSolutionContent = "<tr>";
                    string htcReplyContent = "<td style=\"font-family:Calibri\" >";
                    strReplyId = dsSolution.Rows[i]["ReplyId"].ToString();
                    #region Reply
                    string strReply = dsSolution.Rows[i]["Reply"].ToString().Replace("font-size: x-small", "font-size: 16px");

                    strReply = StringClean(strReply);

                    #endregion
                    htcReplyContent += strReply + "</td>";
                    htmlRowSolutionContent += htcReplyContent + "</tr>";

                    if (ViewBag.ReplyId != null && strReplyId == Convert.ToString(ViewBag.ReplyId))
                    {
                        strDeleteRow += "<tr><td align=\"right\" style=\"color:red;font-weight:bold;font-family:Calibri;font-size:18px;\">";
                        strDeleteRow += "<input type=\"submit\" name=\"Delete\" value=\"Delete\"; onClick=\"DeletePost('" + quesID + "')\" style=\"color:red;font-weight:bold;font-family:Calibri;font-size:18px;border:solid;border-width:1px;border-color:black\">";
                        strDeleteRow += "</td></tr>";
                        Session["DeleteReplyId"] = strReplyId;
                    }

                    tblReplies += htrResponseNoByDetailsOuterRow + strDeleteRow + htmlRowSolutionContent;

                    tblReplies += "<tr><td><br /></td></tr>";

                }
                tblReplies += "</table>";
                model.AnswerDetails = tblReplies;
                ViewBag.AnswerDetails = tblReplies;
            }
            else
            {
                ViewBag.AnswerDetails = null;
            }

            connManager.DisposeConn();
        }
        public ActionResult CreateEditUser(Users user, HttpPostedFileBase fileUserPhoto, string txtPassword)
        {
            string activationCode = Guid.NewGuid().ToString();
            //AddEdit user
            if (Request.Form["Cancel"] == null)
            {
                if (ModelState.IsValid)
                {
                    if (fileUserPhoto != null && fileUserPhoto.ContentLength > 1048576)
                    {
                        ViewBag.Ack = "Image file size should be less than 1 mb";
                        //return;
                    }
                    else
                    {
                        //try
                        //{
                        ConnManager con = new ConnManager();
                        DataSet dsUser = con.GetData("Select * from Users where Email = '" + user.Email + "'");
                        con.DisposeConn();
                        if (dsUser.Tables[0].Rows.Count > 0)
                        {
                            ViewBag.Ack = "EMail id already exists. If you have forgotten password, please click forgot password link on the Sign In page.";
                            return View("Users", user);
                        }

                        DataTable dtUserActivation = con.GetDataTable("select * from UserActivation where  Email = '" + user.Email + "'");
                        if (dtUserActivation.Rows.Count > 0)
                        {
                            ViewBag.lblAck = "User activation pending";
                            ViewBag.Activation = "Resend Activation Code?";
                            return View("../Account/Login");
                        }

                        double dblUserID = 0;
                        SqlConnection LclConn = new SqlConnection();
                        SqlTransaction SetTransaction = null;
                        bool IsinTransaction = false;
                        if (LclConn.State != ConnectionState.Open)
                        {
                            user.SetConnection = user.OpenConnection(LclConn);
                            SetTransaction = LclConn.BeginTransaction(IsolationLevel.ReadCommitted);
                            IsinTransaction = true;
                        }
                        else
                        {
                            user.SetConnection = LclConn;
                        }

                        if (fileUserPhoto != null && fileUserPhoto.FileName != "")
                        {
                            try
                            {
                                string fileName = System.IO.Path.GetFileNameWithoutExtension(fileUserPhoto.FileName);
                                string fileExt = System.IO.Path.GetExtension(fileUserPhoto.FileName);
                                string fullFileName = System.IO.Path.GetFileName(fileUserPhoto.FileName);

                                if (!System.IO.File.Exists(Server.MapPath("~\\Images\\") + fullFileName))
                                {
                                    fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                }
                                else
                                {
                                    fullFileName = fileName + DateTime.Now.ToString("HHmmss") + fileExt;
                                    while (System.IO.File.Exists(fullFileName))
                                    {
                                        fileName = fileName + DateTime.Now.ToString("HHmmss");
                                        fullFileName = fileName + fileExt;
                                    }
                                    fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                }
                                user.ImageURL = "~/Images/" + fullFileName;
                            }
                            catch (Exception ex)
                            {
                                //ViewBag.Ack = "Please try again";
                                user.ImageURL = "~/Images/Person.JPG";
                            }
                        }
                        else
                        {
                            user.ImageURL = "~/Images/Person.JPG";
                        }

                        user.OptionID = 1;
                        user.CreatedDateTime = DateTime.Now;
                        user.Password = txtPassword;

                        bool result = user.CreateUsers(ref dblUserID, SetTransaction);
                        if (IsinTransaction && result)
                        {
                            SetTransaction.Commit();
                        }
                        else
                        {
                            SetTransaction.Rollback();
                        }

                        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCON"].ToString()))
                        {
                            using (SqlCommand cmd = new SqlCommand("INSERT INTO UserActivation VALUES(@UserId, @ActivationCode)"))
                            {
                                using (SqlDataAdapter sda = new SqlDataAdapter())
                                {
                                    cmd.CommandType = CommandType.Text;
                                    cmd.Parameters.AddWithValue("@UserId", dblUserID);
                                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                                    cmd.Connection = conn;
                                    conn.Open();
                                    cmd.ExecuteNonQuery();
                                    conn.Close();
                                }
                            }
                        }

                        user.CloseConnection(LclConn);

                        //ViewBag.Ack = "User Registered Successfully. Please login.";
                        ViewBag.Ack = "User Info Saved Successfully. An activation link has been sent to your email address, please check your inbox and activate your account";
                        //SendNewUserRegEMail(user.Email);
                        SendActivationEMail(user.Email, activationCode);
                        SendEMail(user.Email, user.FirstName, user.LastName);
                    }
                    Session["User"] = user;
                    //return View("ViewUser", user);
                    return Redirect("../Account/ViewUser");
                }
                else
                {
                    ViewBag.Ack = ModelState["ReCaptcha"].Errors[0].ErrorMessage;
                    return View("Users", user);
                }
            }
            else
            {
                return View("Users", user);
            }
        }
        private ActionResult CheckUserLogin(string txtEMailId, string txtPassword)
        {
            ConnManager connManager = new ConnManager();
            connManager.OpenConnection();
            DataTable DSUserList = new DataTable();
            DataTable dtUserActivation = new DataTable();

            if (!string.IsNullOrEmpty(txtPassword))
            {
                DSUserList = connManager.GetDataTable("select * from users where email = '" + txtEMailId + "' and Password = '******'");
            }
            else
            {
                DSUserList = connManager.GetDataTable("select * from users where email = '" + txtEMailId + "'");
            }

            if (DSUserList.Rows.Count == 0)
            {
                ViewBag.lblAck = "Invalid login credentials, please try again";
                return View("../Account/Login");
            }
            else
            {

                dtUserActivation = connManager.GetDataTable("select * from UserActivation where UserId = " + double.Parse(DSUserList.Rows[0]["UserId"].ToString()) + " and Email = '" + txtEMailId + "'");
                if (dtUserActivation.Rows.Count > 0)
                {
                    ViewBag.lblAck = "User activation pending";
                    ViewBag.Activation = "Resend Activation Code?";
                    return View("../Account/Login");
                }

                Users user = new Users();
                user.UserId = double.Parse(DSUserList.Rows[0]["UserId"].ToString());
                user.FirstName = DSUserList.Rows[0]["FirstName"].ToString();
                user.LastName = DSUserList.Rows[0]["LastName"].ToString();
                user.Email = DSUserList.Rows[0]["EMail"].ToString();
                user.Address = DSUserList.Rows[0]["Address"].ToString();
                user.ImageURL = DSUserList.Rows[0]["ImageURL"].ToString();
                user.Password = DSUserList.Rows[0]["Password"].ToString();

                user.ImageURL = user.ImageURL.Replace("~", "");
                user.ImageURL = user.ImageURL.Replace("/CodeAnalyzeMVC2015", "");

                DataSet dsQuestions = new DataSet();
                DataSet dsAnswers = new DataSet();
                DataSet dsArticles = new DataSet();

                dsQuestions = connManager.GetData("Select Count(*) from Question where AskedUser = "******"");
                if (dsQuestions.Tables.Count > 0 && dsQuestions.Tables[0].Rows.Count > 0)
                    user.QuestionsPosted = dsQuestions.Tables[0].Rows[0][0].ToString();

                dsAnswers = connManager.GetData("Select Count(*) from Replies where RepliedUser = "******"");
                if (dsAnswers.Tables.Count > 0 && dsAnswers.Tables[0].Rows.Count > 0)
                    user.AnswersPosted = dsAnswers.Tables[0].Rows[0][0].ToString();

                dsArticles = connManager.GetData("Select Count(*) from CodeArticles where UserId = " + user.UserId + "");
                if (dsArticles.Tables.Count > 0 && dsArticles.Tables[0].Rows.Count > 0)
                    user.ArticlesPosted = dsArticles.Tables[0].Rows[0][0].ToString();
                else
                    user.ArticlesPosted = "0";

                user.Details = DSUserList.Rows[0]["Details"].ToString();
                Session["User"] = user;
                Session["user.Email"] = user.Email;
                ViewBag.UserEmail = user.Email;
                connManager.DisposeConn();
                return View("../Account/ViewUser", user);

            }
        }
        //public ActionResult Google()
        //{
        //    return View();
        //}
        public ActionResult ForgotPassword(string txtEMailId)
        {
            if (!string.IsNullOrEmpty(txtEMailId))
            {
                ConnManager con = new ConnManager();
                DataSet dsUser = con.GetData("Select * from Users where Email = '" + txtEMailId + "'");
                con.DisposeConn();
                if (dsUser.Tables[0].Rows.Count <= 0)
                {
                    ViewBag.Ack = "No such EMail Id exists";
                }

                DataTable dtUserActivation = con.GetDataTable("select * from UserActivation where Email = '" + txtEMailId + "'");
                if (dtUserActivation.Rows.Count > 0)
                {
                    ViewBag.Ack = "User activation pending";
                    ViewBag.Activation = "Resend Activation Code?";
                    return View("../Account/Login");
                }

                if (!string.IsNullOrEmpty(dsUser.Tables[0].Rows[0]["Password"].ToString()))
                {
                    Mail mail = new Mail();
                    mail.IsBodyHtml = true;
                    string EMailBody = System.IO.File.ReadAllText(Server.MapPath("EMailBody.txt"));

                    mail.Body = string.Format(EMailBody, "Your CodeAnalyze account password is " + dsUser.Tables[0].Rows[0]["Password"].ToString());

                    mail.FromAdd = "*****@*****.**";
                    mail.Subject = "Code Analyze account password";
                    mail.ToAdd = dsUser.Tables[0].Rows[0]["EMail"].ToString();
                    mail.SendMail();

                    ViewBag.Ack = "Password has been emailed to you, please check your email.";
                }
                else
                {

                    ViewBag.Ack = "You have created your profile thorugh one of the social sites. Please use the same channel to login. Google Or Facebook";
                }
            }
            return View();
        }
        public ActionResult EditUser(Users user, HttpPostedFileBase fileUserPhoto)
        {
            //AddEdit user
            if (Request.Form["Cancel"] == null)
            {
                if (ModelState.IsValid)
                {
                    if (fileUserPhoto != null && fileUserPhoto.ContentLength > 1048576)
                    {
                        ViewBag.Ack = "Image file size should be less than 1 mb";
                        //return;
                    }
                    else
                    {
                        //try
                        //{
                            ConnManager con = new ConnManager();
                            DataSet dsUser = con.GetData("Select * from Users where Email = '" + user.Email + "'");
                            con.DisposeConn();
                            if (dsUser.Tables[0].Rows.Count > 0)
                            {
                                //if (Session["User"] == null)
                                //{
                                //    ViewBag.Ack = "EMail id already exists. If you have forgotten password, please click forgot password link on the Sign In page.";
                                //    //return;
                                //}
                                user.UserId = double.Parse(dsUser.Tables[0].Rows[0]["UserId"].ToString());
                            }

                            double dblUserID = 0;
                            SqlConnection LclConn = new SqlConnection();
                            SqlTransaction SetTransaction = null;
                            bool IsinTransaction = false;
                            if (LclConn.State != ConnectionState.Open)
                            {
                                user.SetConnection = user.OpenConnection(LclConn);
                                SetTransaction = LclConn.BeginTransaction(IsolationLevel.ReadCommitted);
                                IsinTransaction = true;
                            }
                            else
                            {
                                user.SetConnection = LclConn;
                            }

                            if (fileUserPhoto != null && fileUserPhoto.FileName != "")
                            {
                                try
                                {
                                    string fileName = System.IO.Path.GetFileNameWithoutExtension(fileUserPhoto.FileName);
                                    string fileExt = System.IO.Path.GetExtension(fileUserPhoto.FileName);
                                    string fullFileName = System.IO.Path.GetFileName(fileUserPhoto.FileName);

                                    if (!System.IO.File.Exists(Server.MapPath("~\\Images\\") + fullFileName))
                                    {
                                        fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                    }
                                    else
                                    {
                                        fullFileName = fileName + DateTime.Now.ToString("HHmmss") + fileExt;
                                        while (System.IO.File.Exists(fullFileName))
                                        {
                                            fileName = fileName + DateTime.Now.ToString("HHmmss");
                                            fullFileName = fileName + fileExt;
                                        }
                                        fileUserPhoto.SaveAs(Server.MapPath("~\\Images\\") + fullFileName);
                                    }
                                    user.ImageURL = "~/Images/" + fullFileName;
                                }
                                catch (Exception ex)
                                {
                                //ViewBag.Ack = "Please try again";
                                user.ImageURL = "~/Images/Person.JPG";
                            }
                                user.OptionID = 5;
                            }
                            else
                            {
                                user.OptionID = 7;
                                Users tempUser = new CodeAnalyzeMVC2015.Users();
                                tempUser = (Users)Session["User"];
                                user.ImageURL = tempUser.ImageURL;
                            }

                            user.ModifiedDateTime = DateTime.Now;
                            dblUserID = user.UserId;

                            bool result = user.CreateUsers(ref dblUserID, SetTransaction);
                            if (IsinTransaction && result)
                            {
                                SetTransaction.Commit();
                            }
                            else
                            {
                                SetTransaction.Rollback();
                            }
                            user.CloseConnection(LclConn);

                            ViewBag.Ack = "User Updated Successfully.";
                        //}
                        //catch
                        //{

                        //}
                    }
                    Session["User"] = user;
                    //return View("ViewUser", user);
                    return Redirect("../Account/ViewUser");
                }
                else
                {
                    user = (Users)Session["User"];
                    return View("../Account/ViewUser", user);
                }
            }
            else
            {
                user = (Users)Session["User"];
                return View("../Account/ViewUser", user);
            }
        }