public int verifyLogin(string userName, string pw) //return the role code (1: admin, 2: user, 0: invalid) { try { string command = "select role from NguoiDung where Username = @un and password = @pw"; SqlCommand cmd = new SqlCommand(command, this.con); cmd.Parameters.Add("@un", SqlDbType.VarChar); cmd.Parameters.Add("@pw", SqlDbType.VarChar); cmd.Parameters["@un"].Value = userName; cmd.Parameters["@pw"].Value = pw; con.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (!dr.HasRows) { con.Close(); return(0); } else { dr.Read(); Int16 result = Convert.ToInt16(dr["Role"].ToString()); con.Close(); return(result); } } catch (Exception ex) { MyExceptionNS.MyException myEx = new MyExceptionNS.MyException(); myEx.className = this.GetType().Name; myEx.errMsg = ex.Message; throw myEx; } }
//Write error message to log file. If the file was not created yet, then create it //The pattern for the log file name: log_ddmmyyyy.txt public static void writeLog(MyExceptionNS.MyException ex) { try { if (!Directory.Exists(getLogPath())) { Directory.CreateDirectory(getLogPath()); } string path = getLogPath() + "\\" + generateFileName(); if (!File.Exists(path)) //if the file was not created yet, then create the file and add the header to it { string header = "Log file " + DateTime.Today.ToShortDateString() + Environment.NewLine + "----------------------------------" + Environment.NewLine; File.WriteAllText(path, header); } StringBuilder sb = new StringBuilder(); sb.Append(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString()); sb.AppendLine(" - Error at class: " + ex.className); sb.Append("\t\t"); sb.AppendLine("Error: " + ex.errMsg); File.AppendAllText(path, sb.ToString()); } catch (Exception e) { throw e; } }
//private NguoiDungDTO user; public BLL() { try { this.con = new SqlConnection(CommonFunction.getConnectionString()); } catch (Exception ex) { MyExceptionNS.MyException myEx = new MyExceptionNS.MyException(); myEx.className = this.GetType().Name; myEx.errMsg = ex.Message; throw myEx; } }