public ActionResult DeleteReply(string Id)
 {
     VwSolutionsModel model = new VwSolutionsModel();
     if (Session["DeleteReplyId"] != null)
     {
         if (Id != null)
         {
             ConnManager conn = new ConnManager();
             conn.DeleteReply(Session["DeleteReplyId"].ToString());
             model = SetDefaults();
         }
         Session["DeleteReplyId"] = null;
     }
     return View("../Que/Ans", model);
 }
        public ActionResult InsertAns(string SolutionEditor)
        {
            VwSolutionsModel model = new VwSolutionsModel();
            string strContent = SolutionEditor;
            string strTemp = "";

            if (Session["User"] != null)
            {
                user = (Users)Session["User"];
                string quesID = RouteData.Values["id"].ToString();

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

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

                replies.OptionID = 1;
                replies.QuestionId = double.Parse(quesID.ToString());

                //CleanBeforeInsert(ref SolutionEditor, ref strTemp);

                replies.Reply = SolutionEditor;

                replies.RepliedDate = DateTime.Now;

                if (user.UserId == 1)
                {
                    int[] myy = new int[38] { 16, 17, 18, 19, 23, 24, 25, 26, 32, 34, 35, 37, 39, 40, 41, 42, 44, 45, 46, 47, 48, 51, 52, 54, 55, 56, 57, 58, 59, 63, 69, 70, 71, 72, 73, 82, 104, 106 };
                    Random ran = new Random();
                    int mynum = myy[ran.Next(0, myy.Length)];
                    replies.RepliedUser = mynum;

                    int[] myvotes = new int[12] { 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
                    Random ran2 = new Random();
                    int mynum2 = myvotes[ran2.Next(0, myvotes.Length)];
                    replies.UpVotes = mynum2;

                }
                else
                {
                    replies.RepliedUser = user.UserId;
                }

                bool result = replies.CreateReplies(ref dblReplyID, SetTransaction);

                if (IsinTransaction && result)
                {
                    SetTransaction.Commit();
                }
                else
                {
                    SetTransaction.Rollback();
                }

                replies.CloseConnection(LclConn);
                ViewBag.ReplyId = dblReplyID;
                model = SetDefaults();

                try
                {
                    if (!Session["AskedUserEMail"].ToString().Contains("codeanalyze.com"))
                    {
                        Mail mail = new Mail();

                        string EMailBody = System.IO.File.ReadAllText(Server.MapPath("../../../EMailBody.txt"));

                        System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex("[^a-zA-Z0-9 -]");

                        if (model.QuestionTitle != null)
                        {
                            model.QuestionTitle = rgx.Replace(model.QuestionTitle, "").Replace(" ", "-");
                        }

                        string strLink = "www.codeanalyze.com/Que/Ans/" + quesID.ToString() + "/" + model.QuestionTitle + "";

                        string strBody = "Your question on CodeAnalyse has been answered by one of the users. Check now <a href=" + strLink + "\\>here</a>";

                        mail.Body = string.Format(EMailBody, strBody);

                        mail.FromAdd = "*****@*****.**";
                        mail.Subject = "Code Analyze - Received response for " + model.QuestionTitle;
                        mail.ToAdd = Session["AskedUserEMail"].ToString();
                        mail.CCAdds = "*****@*****.**";
                        mail.IsBodyHtml = true;

                        if (!mail.ToAdd.ToString().ToLower().Equals("*****@*****.**"))
                        {
                            mail.SendMail();
                        }
                    }
                }
                catch(Exception ex)
                {

                }
                //GetQuestionData(quesID.ToString(), ref model);
                //BindSolution("Select * from VwSolutions where QuestionId = " + quesID.ToString(), null);
                //ViewBag.lblAck = string.Empty;
            }
            else
            {
                ViewBag.lblAck = "Please sign in to post your question.";
            }
            return View("../Que/Ans", model);
        }
        private VwSolutionsModel SetDefaults()
        {
            //if (Session["User"] != null)
            //{
            //    user = (Users)Session["User"];
            //    ViewBag.UserEMail = user.Email;
            //}

            string quesID;
            string questionTitle = string.Empty;

            quesID = RouteData.Values["id"].ToString();

            VwSolutionsModel model = new VwSolutionsModel();
            GetQuestionData(quesID.ToString(), ref model);

            questionTitle = model.QuestionTitle;

            if(string.IsNullOrEmpty(questionTitle))
            {
                questionTitle = RouteData.Values["title"].ToString();
            }

            ViewBag.Description = questionTitle.Replace("-", " ");
            ViewBag.keywords = questionTitle.Replace("-", " ");

            if (quesID != null)
            {
                BindSolution("Select * from VwSolutions where QuestionId =" + quesID.ToString(), ref model);
            }

            if (user.Email != null)
            {
                ViewBag.lblAck = string.Empty;
                ViewBag.hfUserEMail = user.Email;
            }
            else
            {
              //  ViewBag.lblAck = "Please sign in to post your answer.";
                ViewBag.hfUserEMail = string.Empty;
            }
            return model;
        }
        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 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 List<VwSolutionsModel> GetSolns(string sqlQuery)
        {
            OpenConnection();
            DataTable DSQuestions = new DataTable();
            DSQuestions = GetDataTable(sqlQuery);
            DisposeConn();

            List<VwSolutionsModel> solns = new List<VwSolutionsModel>();
            VwSolutionsModel soln;
            foreach (DataRow row in DSQuestions.Rows)
            {
                soln = new VwSolutionsModel();
                soln.QuestionID = row["QuestionID"].ToString();
                soln.QuestionTitle = row["QuestionTitle"].ToString();
                soln.AskedUser = row["AskedUser"].ToString();
                soln.RepliedUser = row["RepliedUser"].ToString();
                solns.Add(soln);
            }
            return solns;
        }