an object reference is required for the nonstatic field method or property
[System.Web.Services.WebMethod]
public static void GetCurrentTime(string name)
{
string strFileName = "D://Data.csv";
string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\;Extended Properties='text;HDR=Yes;FMT=Delimited';";
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = connectionstring;
OleDbCommand command = new OleDbCommand();
command.CommandType = CommandType.Text;
command.Connection = connection;
if (name != "")
{
command.CommandText = "select * from " + System.IO.Path.GetFileName(strFileName);// +" where F=" + txtmtcn.Text;
connection.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
dt.Columns[5].ColumnName = "MTCN";
dt.DefaultView.RowFilter = " MTCN =" + name;
dt = dt.DefaultView.ToTable();
TxtSenderFirstName.Text = dt.Rows[0][7].ToString();
开发者_StackOverflow中文版 connection.Close();
}
}
i get Error "an object reference is required for the nonstatic field method or property" how will i access my text box controls and populate data in them.
I guess the error is on this line:
TxtSenderFirstName.Text = dt.Rows[0][7].ToString();
You cannot access instance fields from a static method. In your case you cannot access UI controls from an ASP.NET PageMethod. To achieve this you could modify your page method to return the result and then assign it from the client side to the corresponding field.
精彩评论