G
gordon
Hi
I am learning C# out of books - and soon will be attending some courses. I
like to write a bit of code and see what happens, but I find that I get
stuck at the same spot each time.
my code is simple and has 2 classes - the form class and the getdata class.
When I wish to reference the controls on the form with my getdata classs I
get a message that dgHH (my HH datagrid view) does not exist in the current
context.
I could fool around and find a solution - but I would like to know the best
way to do this.
If you like to show me a better way, taking into account my limited
understanding of C#, I would appreciate it.
The code below does not compile - and there maybe other bugs in it - but I
cant seem to resolve the context one first.
Thanks
Doug
namespace oleClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
public class GetData
{
private void Datagrab()
{
OleDbConnection conn = null;
OleDbDataReader reader = null;
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\hh.mdb";
string strSelect = "Select * from Vars";
try
{
conn = new OleDbConnection(strConnection);
conn.Open();
OleDbCommand cmd = new OleDbCommand(strSelect);
reader = cmd.ExecuteReader();
dgHH.DataSource = reader;
dgHH.DataBind();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
}
}
}
I am learning C# out of books - and soon will be attending some courses. I
like to write a bit of code and see what happens, but I find that I get
stuck at the same spot each time.
my code is simple and has 2 classes - the form class and the getdata class.
When I wish to reference the controls on the form with my getdata classs I
get a message that dgHH (my HH datagrid view) does not exist in the current
context.
I could fool around and find a solution - but I would like to know the best
way to do this.
If you like to show me a better way, taking into account my limited
understanding of C#, I would appreciate it.
The code below does not compile - and there maybe other bugs in it - but I
cant seem to resolve the context one first.
Thanks
Doug
namespace oleClass
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
public class GetData
{
private void Datagrab()
{
OleDbConnection conn = null;
OleDbDataReader reader = null;
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\\hh.mdb";
string strSelect = "Select * from Vars";
try
{
conn = new OleDbConnection(strConnection);
conn.Open();
OleDbCommand cmd = new OleDbCommand(strSelect);
reader = cmd.ExecuteReader();
dgHH.DataSource = reader;
dgHH.DataBind();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (reader != null) reader.Close();
if (conn != null) conn.Close();
}
}
}
}