I have a private method that returns me the result of executing a data set.
What I want to do is reference in this method a textBox that I have in the main button of my Windows Form application. So that when the user manually enters the 'Caption', this new caption is replaced by the one that is already there.
I can call the textBox without problems from button1_click but not from my method.
I get the error an object reference is required to access non-static field method or property
Here is the method in which I am trying to call textBox4.
How could I do that?
static private string GetMappingTable()
{
string SourceID = "";
var ds = GetMappingTable();
foreach (DataTable dst in ds.Tables)
{
foreach (DataRow dr in dst.Rows)
{
//Aqui me aparece el error
if (textBox4.Text != "");
var DataSourceId = ds.Tables["Table"]
.Select("Caption = 'testUSers'")
.Select(r => r["SourceID"])
.Where(s => s != DBNull.Value)
.Select(s => s.ToString())
.FirstOrDefault();
SourceID = DataSourceId;
//Aqui me aparece el error
SourceID = textBox4.Text;
}
}
return SourceID;
}
The problem is that you have marked your method as
static
Static methods do not have access to the fields of the instance since they do not belong to any instance.
Just remove the keyword
static