0

I have susesfully writen code that will open a connection(conn) to the database and then delete all the records in the table. Next comes the part that wont compile or work as expected, conn or connection does not exist in the current context, Ive inserted the errors on the lines in code as well as an possible empty statement possible warning. the code that fails is as far as I can see a mirror off the code above it that compiles and works fine. The code follows.

{
        panel2.Visible = false;
        int i = dataGridView1.CurrentCell.RowIndex;
        i = dataGridView1.SelectedRows[0].Index;
        DataGridViewRow newDataRow = dataGridView1.Rows[i];
        dataGridView1.Rows[i].SetValues(label3.Text, "two", "Three", "Four");

       // string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False";




        String connStr, sql;
        connStr = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False");
        try
        {
            //Empty the table
            sql = "Delete from " + table;
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd1 = new OleDbCommand(sql, conn))
                {
                    cmd1.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            SetCon.Text = SetCon.Text + "Error " + ex + "\n";
        }

            OleDbCommand cmd = new OleDbCommand();
            for (int ii = 0; ii < dataGridView1.Rows.Count; ii++)
            {
                DataGridViewRow dr= dataGridView1.Rows[ii];
                if (dr.Selected == true)
                {
                  string connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False";


                    try
                    {
                        int number = 0; //possible empty statment warning next line
                        using (OleDbConnection connection = new OleDbConnection(connetionString)) ;
                    {
                        sql = "UPDATE EnableOne SET Property=@var1, Pvalue=@var2  Pdefault=@var2 PType=@var2";
                        connection.Open(); //This fail's to compile connection does not exist in the current context
                        using (OleDbCommand cmd1 = new OleDbCommand(sql, connection))//This fail's to compile connection does not exist in the current context
                        {
                            number = dataGridView1.CurrentCell.RowIndex;
                            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                            {
                                cmd1.Parameters.Add(new OleDbParameter("@var1", row.Cells[0].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var2", row.Cells[1].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var3", row.Cells[2].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var4", row.Cells[3].Value.ToString()));
                                cmd1.CommandText = sql;
                                cmd1.ExecuteNonQuery();
                            }

                        }
                    }
                    }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                }

            } }
    }
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
Data
  • 113
  • 2
  • 11
  • cmd1 is defined here : (OleDbCommand cmd1 = new OleDbCommand(sql, conn)). Should be : (cmd1 = new OleDbCommand(sql, conn)). The define at top of module : OleDbCommand cmd1 = null; – jdweng Jul 01 '17 at 16:20
  • (I rolled back the edits because in "tidying up" the code the editor actually *removed* the cause of the problem.) – Gord Thompson Jul 01 '17 at 16:33
  • if I read you right it still does not compile but iam starting to go cross eyed. @jdweng – Data Jul 01 '17 at 16:34
  • I updated code to get rid of compiler errors. – jdweng Jul 01 '17 at 18:49
  • @jdweng - NO. Do not edit the code to remove the *cause* of the errors. That renders the question meaningless. – Gord Thompson Jul 01 '17 at 19:07

0 Answers0