示例#1
0
 private void btnLogin_Click(object sender, EventArgs e)
 {
     MagneticStripCode readWriter; //creates readWriter variable from the MagneticStripCode.cs class
     try {
         readWriter = new MagneticStripCode(); //creates new MagneticStripcCode object and stores in readWriter
         string cardID = readWriter.readCardData().ToString(); //Card ID set to string of whatever is read
         Console.WriteLine(cardID);
         //string last = cardID.Substring(cardID.LastIndexOf('%') + 1);
         string last = Regex.Replace(cardID, "[^0-9]", ""); //Formatted card data without any special characters
         Console.WriteLine("Fixed String: " + last);
         if (last.Equals(null) || last.Equals("")) {
             MessageBox.Show("Error reading card. Press Okay and try again?");
             MagneticStripCode.clearBuffer();
             cardID = readWriter.readCardData().ToString(); //Card ID set to string of whatever is read
             last = Regex.Replace(cardID, "[^0-9]", ""); //Only grab numbers nos pecial characters
             string dbID = Database.checkUser(last)[0]; //Grabs ID from database using the formatted card ID
             Console.WriteLine("DB ID: " + dbID);
             if (last == dbID) { //User does exist
                                 //login
                 Console.WriteLine("User exists");
                 //call method to add to database
                 Database.data(last, true); //Inputs timeclock data to DB
                 string isAdmin = Database.checkUser(last)[9]; //Returns if the user is admin or not
                 if (isAdmin.Equals("1")) { //Is admin
                     foreach (TabPage tab in tabControl1.TabPages) { //Enable all pages
                         tab.Enabled = true;
                     }
                 }
                 lbUserLog.Items.Add("User " + last + " logged in.");
             } else { //User doesn't exist
                 lbUserLog.Items.Add("User " + last + " not found. Contact an administrator.");
             }
         } else {
             string dbID = Database.checkUser(last)[0]; //Grabs ID from database using the formatted card ID
             Console.WriteLine("DB ID: " + dbID);
             if (last == dbID) { //User does exist
                                 //login
                 Console.WriteLine("User exists");
                 //call method to add to database
                 Database.data(last, true);  //Inputs timeclock data to DB
                 string isAdmin = Database.checkUser(last)[9]; //Returns if the user is admin or not
                 if (isAdmin.Equals("1")) {
                     foreach (TabPage tab in tabControl1.TabPages) { //Enable all pages
                         tab.Enabled = true;
                     }
                 }
                 lbUserLog.Items.Add("User " + last + " logged in.");
             } else { //User doesn't exist
                 lbUserLog.Items.Add("User " + last + " not found. Contact an administrator.");
             }
         }
     } catch (Exception ex) { //exception handler
         MessageBox.Show(ex.ToString(), "Something broke in the login. Please contact an administrator."); //catches and displays resulting errors to prevent program crash
     } finally { //finally block runs whether there is an exception or not
         readWriter = null; //sets readWriter object to null after it has been used
         //MagneticStripCode.clearBuffer();
     }
 }