public void updateAppMaster(AppMasterEntity appMasterEntity)
        {
            string sqltext = "UPDATE RBFX.AppMaster2 SET "
                             + "QueueStorageAccount = @p2,"
                             + "QueueStorageKeyEnc = EncryptByPassPhrase(@passPhrase, @p3, 1, CONVERT(varbinary, @p1)),"
                             + "Status = @p4,"
                             + "Description = @p5,"
                             + "Registered_DateTime = @p6 "
                             + "WHERE AppId = @p1";

            SqlConnection conn = new SqlConnection(this.sqlConnectionString);

            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqltext, conn);
                AddSqlParameter(ref cmd, "@p1", SqlDbType.NVarChar, appMasterEntity.AppId);
                AddSqlParameter(ref cmd, "@p2", SqlDbType.NVarChar, appMasterEntity.QueueStorageAccount);
                AddSqlParameter(ref cmd, "@p3", SqlDbType.NVarChar, appMasterEntity.QueueStorageKey);
                AddSqlParameter(ref cmd, "@p4", SqlDbType.NVarChar, appMasterEntity.Status);
                AddSqlParameter(ref cmd, "@p5", SqlDbType.NVarChar, appMasterEntity.Description);
                AddSqlParameter(ref cmd, "@p6", SqlDbType.DateTime, appMasterEntity.Registered_DateTime);
                AddSqlParameter(ref cmd, "@passPhrase", SqlDbType.NVarChar, encPassPhrase);
                cmd.ExecuteNonQuery();

                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            }
        }
        public void insertAppMaster(AppMasterEntity appMasterEntity)
        {
            string sqltext = "INSERT INTO RBFX.AppMaster2 ("
                             + "AppId,QueueStorageAccount,"
                             + "QueueStorageKeyEnc,"
                             + "Status,Description,Registered_DateTime"
                             + ") VALUES ("
                             + "@p1,@p2,"
                             + "EncryptByPassPhrase(@passPhrase, @p3, 1, CONVERT(varbinary, @p1)),"
                             + "@p4,@p5,@p6)";

            SqlConnection conn = new SqlConnection(this.sqlConnectionString);

            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqltext, conn);
                AddSqlParameter(ref cmd, "@p1", SqlDbType.NVarChar, appMasterEntity.AppId);
                AddSqlParameter(ref cmd, "@p2", SqlDbType.NVarChar, appMasterEntity.QueueStorageAccount);
                AddSqlParameter(ref cmd, "@p3", SqlDbType.NVarChar, appMasterEntity.QueueStorageKey);
                AddSqlParameter(ref cmd, "@p4", SqlDbType.NVarChar, appMasterEntity.Status);
                AddSqlParameter(ref cmd, "@p5", SqlDbType.NVarChar, appMasterEntity.Description);
                AddSqlParameter(ref cmd, "@p6", SqlDbType.DateTime, appMasterEntity.Registered_DateTime);
                AddSqlParameter(ref cmd, "@passPhrase", SqlDbType.NVarChar, encPassPhrase);
                cmd.ExecuteNonQuery();

                conn.Close();
            }
            catch (Exception ex)
            {
                conn.Close();
                throw ex;
            }
        }
        private AppMasterEntity getAppMasterEntity()
        {
            AppMasterEntity appMasterEntity = new AppMasterEntity();

            appMasterEntity.AppId = textBoxAppId.Text;
            appMasterEntity.QueueStorageAccount = textBoxStorageAccount.Text;
            appMasterEntity.QueueStorageKey     = textBoxStorageKey.Text;
            appMasterEntity.Status              = comboBoxStatus.Text;
            appMasterEntity.Description         = textBoxDesc.Text;
            appMasterEntity.Registered_DateTime = DateTime.Now;

            return(appMasterEntity);
        }
        public EditAppMasterForm(string sqlConnectionString, string encPassPhrase)
        {
            InitializeComponent();

            this.sqlConnectionString = sqlConnectionString;
            this.encPassPhrase       = encPassPhrase;
            appMasterEntity          = new AppMasterEntity();
            if (encPassPhrase == string.Empty)
            {
                throw (new ApplicationException("Encryption Passphrase is nothing !!"));
            }

            this.createStatus = true;
        }
        private void updateButton_Click(object sender, EventArgs e)
        {
            if (!checkInputData())
            {
                return;
            }

            try
            {
                AppMasterEntity    appMasterEntity    = getAppMasterEntity();
                AppMasterProcessor appMasterProcessor = new AppMasterProcessor(sqlConnectionString, this.encPassPhrase);
                appMasterProcessor.updateAppMaster(appMasterEntity);

                MessageBox.Show("App Master updated successfully!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            this.Close();
        }
        public List <AppMasterEntity> GetAppMasters(string appId)
        {
            appId = appId.Replace("*", "%");

            string sqltext = "SELECT AppId,StorageAccount,"
                             + "CONVERT(NVARCHAR(1000),DecryptByPassphrase(@passPhrase,StorageKeyEnc,1,CONVERT(varbinary,AppId))) AS StorageKey,"
                             + "CONVERT(NVARCHAR(4000),DecryptByPassphrase(@passPhrase,AppInfoEnc,1,CONVERT(varbinary,AppId))) AS AppInfo,"
                             + "CONVERT(NVARCHAR(4000),DecryptByPassphrase(@passPhrase,AppInfoDeviceEnc,1,CONVERT(varbinary,AppId))) AS AppInfoDevice,"
                             + "Status,Description,Registered_DateTime "
                             + "FROM RBFX.AppMaster "
                             + "WHERE AppId LIKE @p1 ORDER BY AppId";

            List <AppMasterEntity> listOfAppMasters = new List <AppMasterEntity>();

            SqlConnection conn = new SqlConnection(this.sqlConnectionString);

            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqltext, conn);
                AddSqlParameter(ref cmd, "@p1", SqlDbType.NVarChar, appId);
                AddSqlParameter(ref cmd, "@passPhrase", SqlDbType.NVarChar, encPassPhrase);
                SqlDataReader reader = cmd.ExecuteReader();

                if (!reader.HasRows)
                {
                    ae = new ApplicationException("No record was found in RBFX.AppMaster");
                    throw ae;
                }

                while (reader.Read())
                {
                    AppMasterEntity appMasterEntity = new AppMasterEntity();
                    appMasterEntity.AppId          = reader.GetString(0);
                    appMasterEntity.StorageAccount = reader.GetString(1);
                    if (!reader.IsDBNull(2))
                    {
                        appMasterEntity.StorageKey = reader.GetString(2);
                    }
                    else
                    {
                        ae = new ApplicationException("Storage key can't be decrypted. Encryption Passphrase may be wrong.");
                        throw ae;
                    }

                    if (!reader.IsDBNull(3))
                    {
                        appMasterEntity.AppInfo = reader.GetString(3);
                    }
                    else
                    {
                        appMasterEntity.AppInfo = string.Empty;
                    }

                    if (!reader.IsDBNull(4))
                    {
                        appMasterEntity.AppInfoDevice = reader.GetString(4);
                    }
                    else
                    {
                        appMasterEntity.AppInfoDevice = string.Empty;
                    }

                    appMasterEntity.Status = reader.GetString(5);

                    if (!reader.IsDBNull(6))
                    {
                        appMasterEntity.Description = reader.GetString(6);
                    }
                    else
                    {
                        appMasterEntity.Description = string.Empty;
                    }

                    if (!reader.IsDBNull(7))
                    {
                        appMasterEntity.Registered_DateTime = reader.GetDateTime(7);
                    }

                    listOfAppMasters.Add(appMasterEntity);
                }

                reader.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                if (ex.GetType().Equals(ae))
                {
                    conn.Close();
                }
                throw (ex);
            }

            return(listOfAppMasters);
        }
        public AppMasterEntity GetAppMaster(string appId)
        {
            AppMasterEntity appMasterEntity = new AppMasterEntity();

            string sqltext = "SELECT AppId,QueueStorageAccount,"
                             + "CONVERT(NVARCHAR(1000),DecryptByPassphrase(@passPhrase,QueueStorageKeyEnc,1,CONVERT(varbinary,AppId))) AS StorageKey,"
                             + "Status,Description,Registered_DateTime "
                             + "FROM RBFX.AppMaster2 "
                             + "WHERE AppId = @p1";

            SqlConnection conn = new SqlConnection(this.sqlConnectionString);

            try
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqltext, conn);
                AddSqlParameter(ref cmd, "@p1", SqlDbType.NVarChar, appId);
                AddSqlParameter(ref cmd, "@passPhrase", SqlDbType.NVarChar, encPassPhrase);
                SqlDataReader reader = cmd.ExecuteReader();

                if (!reader.HasRows)
                {
                    ae = new ApplicationException("No record was found in RBFX.AppMaster2");
                    throw ae;
                }

                reader.Read();

                appMasterEntity.AppId = reader.GetString(0);
                appMasterEntity.QueueStorageAccount = reader.GetString(1);
                appMasterEntity.QueueStorageKey     = reader.GetString(2);

                appMasterEntity.Status = reader.GetString(3);

                if (!reader.IsDBNull(4))
                {
                    appMasterEntity.Description = reader.GetString(4);
                }
                else
                {
                    appMasterEntity.Description = string.Empty;
                }

                if (!reader.IsDBNull(5))
                {
                    appMasterEntity.Registered_DateTime = reader.GetDateTime(5);
                }


                reader.Close();
                conn.Close();
            }
            catch (Exception ex)
            {
                if (ex.GetType().Equals(ae))
                {
                    conn.Close();
                }
                throw (ex);
            }

            return(appMasterEntity);
        }