private VwArticlesModel SetDefaults()
        {
            if (Session["User"] != null)
            {
                user = (Users)Session["User"];
            }

            string articleID;
            string articleTitle = string.Empty;

            articleID = RouteData.Values["Id"].ToString();

            VwArticlesModel model = new VwArticlesModel();
            GetArticleData(articleID.ToString(), ref model);

            articleTitle = model.ArticleTitle.ToString();
            ViewBag.Description = articleTitle.Replace("-", " ");
            ViewBag.keywords = articleTitle.Replace("-", " ");

            if (articleID != null)
            {
                BindComments("Select * from VwArticleReplies where ArticleId =" + articleID.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.lblAck = "";
                ViewBag.hfUserEMail = string.Empty;
            }
            return model;
        }
        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 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();
                    }
                }
            }
        }
        public ActionResult InsertComment(string Comment)
        {
            VwArticlesModel model = new VwArticlesModel();
            if (Session["User"] != null)
            {
                if (!string.IsNullOrEmpty(Comment))
                {
                    user = (Users)Session["User"];
                    string articleID = RouteData.Values["Id"].ToString();

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

                    articleComments.OptionID = 1;
                    articleComments.ReplyUserId = user.UserId;
                    articleComments.ArticleId = double.Parse(articleID.ToString());
                    articleComments.ReplyText = Comment;

                    articleComments.InsertedDate = DateTime.Now;

                    bool result = articleComments.CreateArticleComments(ref dblReplyID, SetTransaction);

                    if (IsinTransaction && result)
                    {
                        SetTransaction.Commit();

                        if (!Session["AskedUserEMail"].ToString().Contains("codeanalyze.com"))
                        {
                            Mail mail = new Mail();
                            string strLink = "www.codeanalyze.com/VA.aspx?QId=" + articleID.ToString() + "&QT=" + model.ArticleTitle + "";
                            mail.Body = "<a href=" + strLink + "\\>Click here to view solution to: " + model.ArticleTitle + "</a>";
                            mail.FromAdd = "*****@*****.**";
                            mail.Subject = "Code Analyze - Received response for " + model.ArticleTitle;
                            mail.ToAdd = Session["AskedUserEMail"].ToString();
                            mail.CCAdds = "*****@*****.**";
                            mail.IsBodyHtml = true;
                            mail.SendMail();
                        }
                    }
                    else
                    {
                        SetTransaction.Rollback();
                    }
                    articleComments.CloseConnection(LclConn);
                    ViewBag.ReplyId = dblReplyID;
                    model = SetDefaults();
                }
                else
                {
                    ViewBag.lblAck = "Please sign in to post your comment.";
                }

            }
            return View("../Articles/Details", model);
        }