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());
}
}
} }
}