-1
[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();
        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.

Rania Umair
  • 1,965
  • 3
  • 18
  • 18

1 Answers1

4

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • but i cant use data table in clint side and in csv file column names are not avail able my file is "Microsoft Office Excel Comma Separated Values File" i was trying to do it in java script but it dont seems possible to read specific data as i am new in development side – Rania Umair Oct 12 '11 at 06:10
  • @RaniaUmair, the querying of the CSV file will still be done on the server, you will only return the result to the client `return dt.Rows[0][7].ToString();`. Then on the client, using javascript, you will assign this result to the corresponding DOM element. – Darin Dimitrov Oct 12 '11 at 06:11
  • i just have to populate values from csv file from javascript it was difficult for me so i call pagemethod in java script but in c# code text box is not accessable – Rania Umair Oct 12 '11 at 06:15