public static UserModel AuthenticateUser(string username, string password)
        {
            if (cn == null)
            {
                cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

            }

            UserModel user = null;

            try
            {

                cn.Open();

                SqlTransaction tn = (SqlTransaction)cn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);

                SqlCommand cmd = new SqlCommand("AuthenticateUser", cn, tn);

                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("Username", username);
                cmd.Parameters.AddWithValue("Password", password);

                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    user = new UserModel()
                    {
                        UserName = (string)reader["Username"].ToString().Trim(),
                        Password = (string)reader["Password"].ToString().Trim(),
                        IsActive = (Boolean)(String.IsNullOrEmpty(reader["IsActive"].ToString()) ? false : Boolean.Parse(reader["IsActive"].ToString())),
                        FirstName = (string)reader["FirstName"].ToString().Trim(),
                        LastName = (string)reader["LastName"].ToString().Trim(),
                        AccountType = (AccountType)Enum.Parse(typeof(AccountType), reader["AccountType"].ToString().ToString().Trim())
                    };
                }

            }
            catch
            {
            }
            finally
            {
                cn.Close();
            }

            return user;
        }
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                UserModel user = new UserModel();

                TryUpdateModel<UserModel>(user, collection.ToValueProvider());

                UserDAO.Insert(user);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public int ImportFromExcel(string xlsFile, UserModel user)
        {
            var numAlpha = new Regex("(?<Alpha>[a-zA-Z]*)(?<Numeric>[0-9]*)");

            String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Data Source=" + xlsFile + ";" +
                "Extended Properties=Excel 8.0;";

            DataSet ds = new DataSet();
            //You must use the $ after the object
            //you reference in the spreadsheet
            OleDbDataAdapter da = new OleDbDataAdapter
            ("SELECT * FROM [Sheet1$]", strConn);

            //da.TableMappings.Add("Table", "ExcelTest");

            da.Fill(ds);

            DataTable dt = ds.Tables[0];

            foreach (DataRow dr in dt.Rows)
            {
                {
                    try
                    {

                        DispatchModel model = new DispatchModel();
                        model.CreateUser = user;

                        try
                        {
                            string time = dr[ExcelMap["Time"]].ToString().Substring(0, 2) + ":" + dr[ExcelMap["Time"]].ToString().Substring(2, 2);
                            string date = dr[ExcelMap["Date"]].ToString().Split(' ')[0];
                            model.CreateDate = DateTime.Parse(time + " " + date);
                        }
                        catch
                        {
                            throw;
                        }

                        model.Unit = StringToNumber(dr[ExcelMap["Unit"]].ToString());
                        InitialDiagnosisModel ic = new InitialDiagnosisModel();

                        string[] ageString = dr[ExcelMap["Age"]].ToString().Split(' ');
                        if (ageString.Length > 1)
                        {

                            var match = numAlpha.Match(dr[ExcelMap["Age"]].ToString());

                            ic.Age = StringToNumber(match.Groups["Numeric"].ToString());

                            string alpha = "";

                            if (match.Groups["Alpha"].Length == 0)
                                alpha = dr[ExcelMap["Age"]].ToString().Replace(match.Groups["Numeric"].ToString(), "");

                            switch (alpha.ToLower().Trim())
                            {
                                case "mos":
                                    ic.AgeType = AgeType.Months;
                                    break;
                                default:
                                    ic.AgeType = AgeType.Years;
                                    break;
                            }
                        }
                        else
                        {
                            ic.Age = StringToNumber(dr[ExcelMap["Age"]].ToString());
                            ic.AgeType = AgeType.Years;
                        }

                        switch (dr[ExcelMap["Sex"]].ToString().ToUpper().Trim())
                        {
                            case "M":
                                ic.Gender = Gender.Male;
                                break;
                            case "F":
                                ic.Gender = Gender.Female;
                                break;
                            default:
                                ic.Gender = Gender.Unknown;
                                break;
                        }

                        try
                        {
                            ic.Category = (Category)Enum.Parse(typeof(Category), dr[ExcelMap["Category"]].ToString());
                        }
                        catch
                        {
                            ic.Category = Category.OTHER;
                        }

                        ic.ChiefComplaint = dr[ExcelMap["CC"]].ToString();

                        string[] bp = dr[ExcelMap["BP"]].ToString().Split(' ');

                        if (bp.Length > 0)
                        {
                            ic.BP1 = new BloodPressure(bp[0]);
                            if (bp.Length > 1)
                            {
                                ic.BP2 = new BloodPressure(bp[1]);
                            }
                        }

                        string[] pulse = dr[ExcelMap["Pulse"]].ToString().Split('-');

                        if (pulse.Length > 0)
                        {
                            ic.Pulse1 = StringToNumber(pulse[0]);

                            if (pulse.Length > 1)
                            {
                                ic.Pulse2 = StringToNumber(pulse[1].Trim());
                            }
                        }

                        string[] Resp = dr[ExcelMap["Resp"]].ToString().Split('-');

                        if (Resp.Length > 0)
                        {
                            ic.Resp1 = StringToNumber(Resp[0]);

                            if (Resp.Length > 1)
                            {
                                ic.Resp2 = StringToNumber(Resp[1].Trim());
                            }
                        }
                        string[] O2Sat = dr[ExcelMap["O2"]].ToString().Split('-');

                        if (O2Sat.Length > 0)
                        {
                            ic.O2Sat = StringToNumber(O2Sat[0]);

                            if (O2Sat.Length > 1)
                            {
                                ic.O2Sat2 = StringToNumber(O2Sat[1].Trim());
                            }
                        }

                        string[] BLG = dr[ExcelMap["BLG"]].ToString().Split('-');

                        if (BLG.Length > 0)
                        {
                            ic.BLG1 = StringToNumber(BLG[0]);

                            if (BLG.Length > 1)
                            {
                                ic.BLG2 = StringToNumber(BLG[1].Trim());
                            }
                        }

                        switch (dr[ExcelMap["LOC"]].ToString().Trim().ToLower())
                        {
                            case "y":
                                ic.LossOfConciousness = true;
                                break;
                            default:
                                ic.LossOfConciousness = false;
                                break;
                        }

                        ic.GSC1 = StringToNumber(dr[ExcelMap["GSC"]].ToString());

                        AlertsModel alerts = new AlertsModel();

                        switch (dr[ExcelMap["Trauma"]].ToString().ToLower().Trim())
                        {
                            case "y":
                                alerts.TraumaAlert = true;
                                break;
                            default:
                                alerts.TraumaAlert = false;
                                break;
                        }

                        switch (dr[ExcelMap["Stroke"]].ToString().ToLower().Trim())
                        {
                            case "y":
                                alerts.StrokeAlert = true;
                                break;
                            default:
                                alerts.StrokeAlert = false;
                                break;
                        }

                        switch (dr[ExcelMap["Stemi"]].ToString().ToLower().Trim())
                        {
                            case "y":
                                alerts.STEMI = true;
                                break;
                            default:
                                alerts.STEMI = false;
                                break;
                        }

                        alerts.ETA = StringToNumber(numAlpha.Match(dr[ExcelMap["ETA"]].ToString()).Groups["Numeric"].ToString());

                        MedicalDetailModel md = new MedicalDetailModel();

                        try
                        {
                            md.TC_ER__PEDS = (DispatchDestination)Enum.Parse(typeof(DispatchDestination), dr[ExcelMap["Destination"]].ToString());
                        }
                        catch
                        {
                        }
                        md.Level = dr[ExcelMap["Level"]].ToString();

                        model.InitialCondition = ic;
                        model.Alerts = alerts;
                        model.MedicalDetail = md;

                        DispatchDAO.Insert(model);

                    }
                    catch
                    {
                    }
                }
            }

            return 1;
        }
        public static int Update(UserModel user)
        {
            if (cn == null)
            {
                cn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

            }

            try
            {

                cn.Open();

                SqlTransaction tn = (SqlTransaction)cn.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);

                SqlCommand cmd = new SqlCommand("UpdateUser", cn, tn);

                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("Username", user.UserName);
                cmd.Parameters.AddWithValue("Password", user.Password);
                cmd.Parameters.AddWithValue("Firstname", user.FirstName);
                cmd.Parameters.AddWithValue("Lastname", user.LastName);
                cmd.Parameters.AddWithValue("IsActive", user.IsActive);
                cmd.Parameters.AddWithValue("AccountType", user.AccountType);

                return cmd.ExecuteNonQuery();
            }
            catch
            {
                //implement error logging
            }
            finally
            {
                cn.Close();
            }

            return -1;
        }
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                UserModel user = new UserModel();
                TryUpdateModel(user, collection.ToValueProvider());

                UserDAO.Update(user);

                return RedirectToAction("Details", new { id = id });
            }
            catch
            {
                return View();
            }
        }