Beispiel #1
0
        private void btnAdminOk_Click(object sender, EventArgs e)
        {
            try
            {
                string DecryptKey = "admin";

                byte[] keyBytes = Encoding.UTF8.GetBytes(DecryptKey);

                string keyBase64 = Convert.ToBase64String(keyBytes);

                DataRow adminRow = DatabaseConnectionObject.datasetObject_Global.Tables["System Users"].Rows.Find("admin");

                string decryptedAdmin = new EncAndDec(keyBase64).dencryptData(adminRow["Password"].ToString());

                if (tbxAdminPassword.Text == decryptedAdmin)
                {
                    loginOk = true;

                    //MessageBox.Show("SuccessFull!", "Login successfull");

                    this.Close();
                }

                else
                {
                    MessageBox.Show("Invalid Password", "Error");
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("Admin Login Error: " + ex.Message, "Error");
            }
        }
        private void btnBackup_Click(object sender, EventArgs e)
        {
            FileStream   fsBackup = null;
            StreamWriter sWriter  = null;
            string       EncKey   = Convert.ToBase64String(Encoding.UTF8.GetBytes("admin1"));



            string BackupfilePath = Path.GetDirectoryName(Application.ExecutablePath) + "\\files\\Backupfile.bkp";

            StringBuilder sb = new StringBuilder();

            try
            {
                sb.Clear();

                sb.AppendLine("<BACKUP");
                sb.AppendLine("         <SESSION_ID>|" + GlobalVariables.CurrentSessionID + "|</SESSION_ID>");
                sb.AppendLine("         <BACKUP_DATE>|" + DateTime.Now.ToLocalTime().ToString() + "|<BACKUP_DATE>");
                sb.AppendLine("         <DATA>");

                DataRow[] BackupRows = new DataRow[DatabaseConnectionObject.datasetObject_Global.Tables["Backupsessions"].Rows.Count];
                DatabaseConnectionObject.datasetObject_Global.Tables["Backupsessions"].Rows.CopyTo(BackupRows, 0);

                for (int i = 0; i < BackupRows.Length; i++)
                {
                    String       Source_TableName   = BackupRows[i]["TableName"].ToString();
                    DataTable    table              = DatabaseConnectionObject.datasetObject_Global.Tables[Source_TableName];
                    String       Action             = BackupRows[i]["Action"].ToString();
                    String       RowID              = BackupRows[i]["ItemID"].ToString();
                    String       TransactionDate    = BackupRows[i]["TransactionDate"].ToString();
                    DataColumn[] CurrentTableColums = new DataColumn[] { };

                    DataRow Source_TableRow = null;
                    if (Action == "DELETE")
                    {
                        Source_TableRow = table.NewRow();
                    }
                    else
                    {
                        Source_TableRow = DatabaseConnectionObject.datasetObject_Global.Tables[Source_TableName].Rows.Find(RowID);
                    }

                    if (Source_TableRow == null)
                    {
                        throw new Exception("Backup CANNOT continue because ROW with ID: >> " + RowID + " << In Table: >> " + Source_TableName + " << was NOT FOUND! Contact Database Administrator");
                    }

                    String sb_Line = "              DATE:" + new DateTimeUtility().ToEpochFormat(DateTime.Now.ToLocalTime()) + "|TRANSACTION_DATE:" + TransactionDate + "|TABLE_NAME:" + Source_TableName + "|ROW_ID:" + RowID + "|ACTION:" + Action + "|DATE:[";
                    //table.Columns.CopyTo(CurrentTableColums, 0);

                    String DataLine = "";

                    switch (Action)
                    {
                    case "CREATE":

                        foreach (DataColumn dtCol in table.Columns)
                        {
                            DataLine = DataLine + dtCol.ColumnName + "=" + Source_TableRow[dtCol.ColumnName].ToString() + ",";
                        }

                        break;

                    case "EDIT":
                        foreach (DataColumn dtCol in table.Columns)
                        {
                            DataLine = DataLine + dtCol.ColumnName + "=" + Source_TableRow[dtCol.ColumnName].ToString() + ",";
                        }
                        break;

                    case "DELETE":

                        DataLine = DataLine + " ";

                        break;

                    default:

                        throw new Exception("Action: " + Action + " Not Recognised!");
                    }

                    sb_Line = sb_Line + DataLine.TrimEnd(new char[] { ',' }) + "]";


                    sb.AppendLine(new EncAndDec(EncKey).encryptData(sb_Line));
                }

                sb.AppendLine("         </DATA>");
                sb.AppendLine("</BACKUP>");

                String EncryptedPass = new EncAndDec(EncKey).encryptData("admin");
                String EncryptedData = new EncAndDec(EncKey).encryptData(sb.ToString());

                ServiceRef.ShabaLiteServiceClient webClient = new ServiceRef.ShabaLiteServiceClient();

                ServiceRef.ResponseMessage respMess = (ServiceRef.ResponseMessage)webClient.BackupData(EncryptedPass + "|" + EncryptedData);

                if (respMess.Status == "0" && respMess.Status != null)
                {
                    // To Implement: clear backp table......
                    // ....also updates relevent rows in settings table
                    TextWriter txt = File.CreateText(BackupfilePath);
                    txt.Write(sb.ToString());
                    txt.Close();

                    MessageBox.Show("BackUp as SccessFull!", "SUcess!");
                }
                else
                {
                    throw new Exception("Server Error: " + respMess.ErrorMessage);
                }
            }
            catch (Exception ex) {
                if (fsBackup != null)
                {
                    fsBackup.Close();
                }
                MessageBox.Show("Reason: " + ex.Message.ToString(), "Error Backing up");
            }
        }