コード例 #1
0
ファイル: Utilities.cs プロジェクト: tnhhcmus/Projects
        public static string ReplaceImageUri(string html, string imageDir)
        {
            string tag = "[Utilities][ReplaceImageUri]";

            try
            {
                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.OptionAutoCloseOnEnd = true;
                htmlDoc.LoadHtml(html);

                foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//img[@src]"))
                {
                    var src = node.Attributes["src"].Value.Split('?')[0];
                    if (!src.Contains("://"))
                    {
                        src = src.Replace(imageDir, "");
                    }
                    else
                    {
                        src = SaveFileToServer(src, imageDir);
                    }

                    //var width = node.Attributes["width"].Value.Replace("px", "");
                    //var height = node.Attributes["height"].Value.Replace("px", "");


                    node.SetAttributeValue("src", src);

                    //node.SetAttributeValue("src", string.Format(
                    //    "{0}?width={1}&height={2}",
                    //    src,
                    //    width == null ? "auto" : width,
                    //    height == null ? "auto" : height));
                }

                return(htmlDoc.DocumentNode.OuterHtml);
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());
                return(html);
            }
        }
コード例 #2
0
ファイル: Utilities.cs プロジェクト: tnhhcmus/Projects
        public static string UploadFileToServer(string address, string dir)
        {
            string tag = "[Utilities][SaveFileToServer]";

            LogHelpers.WriteStatus(tag, string.Format("Address: {0} - Dir: {1}",
                                                      address,
                                                      dir),
                                   "Start...");

            try
            {
                string ext      = Path.GetExtension(address);
                string folder   = DateTime.Now.ToString("MMddyyyy");
                string fileName = DateTime.Now.ToString("HHmmssfff") + ext;
                string result   = Path.Combine(folder, fileName);
                //---

                string dirServer = HttpContext.Current.Server.MapPath(Path.Combine(dir, folder));
                if (!Directory.Exists(dirServer))
                {
                    Directory.CreateDirectory(dirServer);
                }

                string fullPath = Path.Combine(dirServer, fileName);

                WebClient wc = new WebClient();
                wc.DownloadFile(address, fullPath);

                return(result.Replace("\\", "/"));
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());
                return(string.Empty);
            }
            finally
            {
                LogHelpers.WriteStatus(tag, string.Format("Address: {0} - Dir: {1}",
                                                          address,
                                                          dir),
                                       "End.");
            }
        }
コード例 #3
0
        public static string SetFullLinkImage(string html, string rootImageDir)
        {
            string tag = "[Utilities][SetFullLinkImage]";

            if (string.IsNullOrEmpty(html))
            {
                return("");
            }

            try
            {
                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.OptionAutoCloseOnEnd = true;
                htmlDoc.LoadHtml(html);

                foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//img[@src]"))
                {
                    var src = node.Attributes["src"].Value.Split('?')[0];
                    if (src.Contains("://"))
                    {
                        continue;
                    }

                    string fullPath = Path.Combine(rootImageDir, src);
                    node.SetAttributeValue("src", fullPath);

                    //node.SetAttributeValue("src", string.Format(
                    //    "{0}?width={1}&height={2}",
                    //    src,
                    //    width == null ? "auto" : width,
                    //    height == null ? "auto" : height));
                }

                return(htmlDoc.DocumentNode.OuterHtml);
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());
                return(html);
            }
        }
コード例 #4
0
        public static ResultBOL <DataSet> ExecuteStoredReturnDataSet(string stored, Object obj)
        {
            string tag = _tag + "[ExecuteStoredReturnDataSet]";

            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString());
                SqlCommand    cmd  = CreateSqlCommandStored(conn, stored, Utilities.GetParameters(obj));

                SqlParameter dbReturn = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
                dbReturn.Direction = ParameterDirection.ReturnValue;

                conn.Open();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet        ds = new DataSet();
                da.Fill(ds);

                cmd.Dispose();
                conn.Close();

                return(new ResultBOL <DataSet>()
                {
                    Code = 0,
                    DbReturnValue = (int)dbReturn.Value,
                    Data = ds
                });
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());

                return(new ResultBOL <DataSet>()
                {
                    Code = ex.HResult,
                    ErrorMessage = ex.Message,
                    Data = null
                });
            }
        }
コード例 #5
0
        private static SqlCommand CreateSqlCommandStored(SqlConnection conn, string stored, Hashtable hashTable)
        {
            string tag = _tag + "[CreateSqlCommandStored]";

            try
            {
                SqlCommand cmd = new SqlCommand(stored, conn);
                cmd.CommandType    = CommandType.StoredProcedure;
                cmd.CommandTimeout = _commandTimeout;

                if (hashTable == null || hashTable.Count == 0)
                {
                    return(cmd);
                }

                foreach (DictionaryEntry entry in hashTable)
                {
                    if (entry.Key == null || string.IsNullOrEmpty(entry.Key.ToString()))
                    {
                        continue;
                    }

                    string key   = "@" + entry.Key.ToString();
                    object value = entry.Value == null ? DBNull.Value : entry.Value;

                    SqlParameter para = new SqlParameter(key, value);
                    cmd.Parameters.Add(para);
                }

                return(cmd);
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());

                return(null);
            }
        }
コード例 #6
0
ファイル: Utilities.cs プロジェクト: tnhhcmus/Projects
        public static string SaveMutipleFileToServer(string[] images, string dir)
        {
            string tag = "[Utilities][Utilities]";

            LogHelpers.WriteStatus(tag, "Start...");
            //---
            string result = string.Empty;

            try
            {
                foreach (string filePath in images)
                {
                    string uploadFile = UploadFileToServer(filePath, dir);
                    //---
                    if (string.IsNullOrEmpty(result))
                    {
                        result = uploadFile;
                    }
                    else
                    {
                        result += "|" + uploadFile;
                    }
                }

                return(result);
            }
            catch (Exception ex)
            {
                LogHelpers.WriteException(tag, ex.ToString());

                return(result);
            }
            finally
            {
                LogHelpers.WriteStatus(tag, "End.");
            }
        }
コード例 #7
0
        private void StartCheckLogin()
        {
            try
            {
                lbError.InnerText = "";
                lbError.Visible   = false;
                string username = tbxUsername.Text;
                string password = tbxPassword.Text;

                var result = UsersDAL.Login(username, password);
                if (result.Code < 0 || result.DbReturnValue < 0)
                {
                    lbError.InnerText = result.ErrorMessage;
                    lbError.Visible   = true;
                    return;
                }

                if (result.Data.Tables.Count == 0 ||
                    result.Data.Tables[0].Rows.Count == 0)
                {
                    lbError.InnerText = "Sai tên đăng nhập hoặc mật khẩu không đúng.";
                    lbError.Visible   = true;
                    return;
                }

                UserBOL BOL = new UserBOL(result.Data.Tables[0].Rows[0]);
                Utilities.SaveLoggedInfo(BOL);
                //---
                GoToHome();
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError("[Default][btnLogin_Click] Exception: {0}", ex.ToString());
                lbError.InnerText = ex.Message;
                lbError.Visible   = true;
            }
        }
コード例 #8
0
        public static ResultBOL <int> ExecuteStored(string stored, object obj)
        {
            string tag = _tag + "[ExecuteStored]";

            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString());
                SqlCommand    cmd  = CreateSqlCommandStored(conn, stored, Utilities.GetParameters(obj));

                SqlParameter dbReturn = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
                dbReturn.Direction = ParameterDirection.ReturnValue;

                conn.Open();

                int exec = cmd.ExecuteNonQuery();

                cmd.Dispose();
                conn.Close();
                conn.Dispose();

                return(new ResultBOL <int>()
                {
                    Code = (int)dbReturn.Value,
                    DbReturnValue = (int)dbReturn.Value
                });
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());

                return(new ResultBOL <int>()
                {
                    Code = ex.HResult,
                    ErrorMessage = ex.Message
                });
            }
        }
コード例 #9
0
        public static ResultBOL <DataSet> ExecuteQuery(string query)
        {
            string tag = _tag + "[ExecuteQuery]";

            try
            {
                SqlConnection conn = new SqlConnection(ConnectionString());
                SqlCommand    cmd  = new SqlCommand(query, conn);
                cmd.CommandTimeout = _commandTimeout;
                conn.Open();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet        ds = new DataSet();
                da.Fill(ds);
                da.Dispose();

                cmd.Dispose();
                conn.Close();

                return(new ResultBOL <DataSet>()
                {
                    Code = 0,
                    Data = ds
                });
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(tag, ex.ToString());

                return(new ResultBOL <DataSet>()
                {
                    Code = ex.HResult,
                    ErrorMessage = ex.Message,
                    Data = null
                });
            }
        }
コード例 #10
0
        private ProjectBOL CreateNewProject()
        {
            string tag = _tag + "[CreateNewProject]";

            try
            {
                DateTime startDate = DateTime.MinValue;
                if (!string.IsNullOrEmpty(tbxStartDate.Text) &&
                    !DateTime.TryParseExact(tbxStartDate.Text, "MM/dd/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out startDate))
                {
                    startDate = DateTime.MinValue;
                }

                DateTime endDate = DateTime.MinValue;
                if (!string.IsNullOrEmpty(tbxEndDate.Text) &&
                    !DateTime.TryParseExact(tbxEndDate.Text, "MM/dd/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out endDate))
                {
                    endDate = DateTime.MinValue;
                }

                ProjectBOL BOL = new ProjectBOL()
                {
                    Name_VN        = tbxNameVN.Text,
                    Description_VN = tbxDesVN.Text,
                    Content_VN     = txaContentVN.Text,
                    Address_VN     = tbxAddress_VN.Text,
                    //---
                    Name_EN        = tbxNameEN.Text,
                    Description_EN = tbxDesEN.Text,
                    Address_EN     = tbxAddress_EN.Text,
                    Content_EN     = txaContentEN.Text,
                    StartDate      = startDate,
                    EndDate        = endDate,
                    //IsRedirectUrl = chkChooseCategory.Checked ? "Yes" : "No",
                    //RedirectUrl = tbxRedirectLink.Text.Trim(),
                    //--
                    ImageLink = hfListImage.Value,
                    //---
                    InsertDate  = DateTime.Now,
                    UpdatedDate = DateTime.Now
                };
                //---
                int menuId = Utilities.GetRequestParameter("MenuId");
                if (menuId > 0)
                {
                    BOL.MenuId = menuId;
                }

                int id = Utilities.GetRequestParameter("Id");
                if (id > 0)
                {
                    BOL.Id = id;
                }

                return(BOL);
            }
            catch (Exception ex)
            {
                LogHelpers.WriteError(string.Format("{0} Exception: {1}", tag, ex.ToString()));
                return(null);
            }
        }