Trouble updating DataGridView.Datasource in a different class - Error "An object reference is required for the non-static field, method, or property"
-
this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.
namespace LockOut
{
public partial class Utility : Form
{
public Form1 m_parent;
public Utility(Form1 frm1)
{
m_parent = frm1;
}
public static DataTable DeviceHistory()
{
OleDbConnection pcDataConn = DBConnection();
try
{
DataSet ds = new DataSet();
string SQL = "SQL Statement";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
adapter.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count < 1)
{
dt = null;
}
pcDataConn.Close();
return dt;
}
catch(Exception ex)
{
pcDataConn.Close();
DataTable dt = null;
Console.WriteLine(ex.Message.ToString());
return dt;} } public static void UpdateDeviceHistory() { **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated }
}
namespace LockOut
{
public partial class Form1 : Form
{
private void Refresh_Click(object sender, EventArgs e)
{
Utility.UpdateDeviceHistory();
}
}
} -
this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.
namespace LockOut
{
public partial class Utility : Form
{
public Form1 m_parent;
public Utility(Form1 frm1)
{
m_parent = frm1;
}
public static DataTable DeviceHistory()
{
OleDbConnection pcDataConn = DBConnection();
try
{
DataSet ds = new DataSet();
string SQL = "SQL Statement";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
adapter.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count < 1)
{
dt = null;
}
pcDataConn.Close();
return dt;
}
catch(Exception ex)
{
pcDataConn.Close();
DataTable dt = null;
Console.WriteLine(ex.Message.ToString());
return dt;} } public static void UpdateDeviceHistory() { **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated }
}
namespace LockOut
{
public partial class Form1 : Form
{
private void Refresh_Click(object sender, EventArgs e)
{
Utility.UpdateDeviceHistory();
}
}
} -
this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.
namespace LockOut
{
public partial class Utility : Form
{
public Form1 m_parent;
public Utility(Form1 frm1)
{
m_parent = frm1;
}
public static DataTable DeviceHistory()
{
OleDbConnection pcDataConn = DBConnection();
try
{
DataSet ds = new DataSet();
string SQL = "SQL Statement";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
adapter.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count < 1)
{
dt = null;
}
pcDataConn.Close();
return dt;
}
catch(Exception ex)
{
pcDataConn.Close();
DataTable dt = null;
Console.WriteLine(ex.Message.ToString());
return dt;} } public static void UpdateDeviceHistory() { **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated }
}
namespace LockOut
{
public partial class Form1 : Form
{
private void Refresh_Click(object sender, EventArgs e)
{
Utility.UpdateDeviceHistory();
}
}
}Why does
Utility
inherit fromForm
? If it really is a 'Utility' class, does it need to inherit fromForm
? In order to give the most appropriate answer, it would help to know this. In general the problem is that your methods are class methods (static), but your fields are not. Non-static items can only be accessed through an instance of a class, not through the class itself. [Mod] It took me so long to type this that your question has already been answered. [/Mod]Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
this is my first C# project so bear with me. Here is the code. HistoryDataView is part of Form1 and is set to public.
namespace LockOut
{
public partial class Utility : Form
{
public Form1 m_parent;
public Utility(Form1 frm1)
{
m_parent = frm1;
}
public static DataTable DeviceHistory()
{
OleDbConnection pcDataConn = DBConnection();
try
{
DataSet ds = new DataSet();
string SQL = "SQL Statement";
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(SQL, pcDataConn);
adapter.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count < 1)
{
dt = null;
}
pcDataConn.Close();
return dt;
}
catch(Exception ex)
{
pcDataConn.Close();
DataTable dt = null;
Console.WriteLine(ex.Message.ToString());
return dt;} } public static void UpdateDeviceHistory() { **m\_parent.HistoryDataView.DataSource** = DeviceHistory(); //<-- Here is where the error is generated }
}
namespace LockOut
{
public partial class Form1 : Form
{
private void Refresh_Click(object sender, EventArgs e)
{
Utility.UpdateDeviceHistory();
}
}
}Your UpdateDeviceHistory method is static. But you are trying to access m_parent. You can't do that. Because m_parent is not static. To be more specific context of your program is needed but you could do this at least: 1. Remove static from your UpdateDeviceHistory method. 2. create Utility class instance and call UpdateDeviceHistory on that instance, that is: Utility utility = new Utility(some_form_with_HistoryDataView) utility.UpdateDeviceHistory() Good Luck
Vitaliy Tsvayer Tikle
-
Any reason the Utility class is a form? I would make Utility a regular class and remove the UpdateDeviceHistory method. Then in Form1 i would do this:
HistoryDataView.DataSource = Utility.DeviceHistory();
That is actually how I had it running initially within Form1. But I have another method within the Utility Class That logs the transactions and I wanted to be able to just update the datagridview after every log, so I would still need to use it from this class. I just turned it into a button click for testing. I hope this makes sense.
-
That is actually how I had it running initially within Form1. But I have another method within the Utility Class That logs the transactions and I wanted to be able to just update the datagridview after every log, so I would still need to use it from this class. I just turned it into a button click for testing. I hope this makes sense.