shah Momin

shah Momin

  • 2k
  • 172
  • 8.8k

Fix Object Value

May 25 2022 7:31 PM
 private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("S#");
            dt.Columns.Add("FileName");
            dt.Columns.Add("Status");
            dt.Columns.Add("ErrorMessage");
            txtFolderPath.Text = ConfigurationManager.AppSettings["FolderPath"].ToString();
            
            
        }

        private void LoadFiles(string path)
        {
            int num = 1;
            DataTable dt = new DataTable();
            foreach(string file in Directory.GetFiles(path))
            {
                string fileName = Path.GetFileName(file);
                dt.Rows.Add((object)num++, (object)fileName, (object)"", (object)"");
            }
            dgvExecuteStatus.DataSource = (object)dt;
            dgvExecuteStatus.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgvExecuteStatus.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgvExecuteStatus.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dgvExecuteStatus.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }

        private void ExecuteScripts(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker backgroundWorker = sender as BackgroundWorker;
            DataTable dataTable = (DataTable)(e.Argument as Dictionary<string, object>)["DataTable"];
            try
            {
                //
            }
            catch (Exception ex)
            {
                int num = (int)MessageBox.Show(ex.Message, "Database Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            int count = this.dgvExecuteStatus.Rows.Count;
            for (int index = 0; index < count; ++index)
            {
                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                backgroundWorker.ReportProgress((index + 1) * 100 / count, (object)((index + 1).ToString() + " of " + (object)count + " scripts executed..."));
                DataGridViewRow row = this.dgvExecuteStatus.Rows[index];
                string path = this.txtFolderPath.Text + "\\" + row.Cells[1].Value.ToString();
                try
                {
                    string script = File.ReadAllText(path).Replace("SET ANSI_NULLS ON", "").Replace("GO", "").Replace("SET QUOTED_IDENTIFIER ON", "").Replace("ALTER", "CREATE");
                    //objDAL.ExecuteScript(script);
                    Thread.Sleep(1000);
                    //objDAL.ExecuteScript(script.Replace("CREATE", "ALTER"));
                    string str = "Success";
                    row.Cells[2].Value = (object)str;
                    row.Cells[2].Style.BackColor = Color.Green;
                    row.Cells[2].Style.ForeColor = Color.White;
                }
                catch (Exception ex)
                {
                    row.Cells[2].Value = (object)"Failed";
                    row.Cells[2].Style.BackColor = Color.Red;
                    row.Cells[2].Style.ForeColor = Color.White;
                    row.Cells[3].Value = (object)ex.Message;
                }
                Thread.Sleep(2000);
            }
            backgroundWorker.ReportProgress(100, (object)"Scripts execution completed");
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = this.txtFolderPath.Text;
            if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
                return;
            txtFolderPath.Text = folderBrowserDialog.SelectedPath;
           LoadFiles(this.txtFolderPath.Text);
        }
        
        private void btnExecute_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            btnExecute.Enabled = false;
            LoadingBar loadingBar = new LoadingBar("Executing Scripts", new DoWorkEventHandler(ExecuteScripts), new Dictionary<string, object>()
            {
                {
                    "DataTable",(object)dt
                }
            });
            int num1 = (int)loadingBar.ShowDialog();
            if (!(loadingBar.TaskStatus == "Cancelled"))
                return;
            int num2 = (int)MessageBox.Show("Execution cancelled", "Scripts Execution", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

string fileName = Path.GetFileName(file);

  dt.Rows.Add((object)num++, (object)fileName, (object)"", (object)"");

iam Highlit  which value in object Value like textbox or lable or path ,filename 


Answers (2)