views:

30

answers:

1
SqlDataReader reader;
string r="";
if ((FileUpload1.PostedFile != null)&&(FileUpload1.PostedFile.ContentLength > 0))
{
    r = System.IO.Path.GetFullPath(FileUpload1.PostedFile.FileName);
}
OleDbConnection oconn =
    new OleDbConnection
    (@"Provider=Microsoft.Jet.OLEDB.4.0;"
    + @"Data Source="+r+";"
    + @"Extended Properties=""Excel 8.0;HDR=Yes;""");
oconn.Open();
OleDbCommand dbcom = new OleDbCommand("SELECT * FROM [Sheet1$]", oconn);
OleDbDataReader dbreader = dbcom.ExecuteReader();
int rni = dbreader.GetOrdinal ("RollNo");
int mki = dbreader.GetOrdinal ("marks");

Here the program works only if the excel sheet is present in the project folder. And if any changes are made to the excel sheet and then the program is run again, then only the rows that were present before are fetched, and not the ones added later. Please help me.....thanks in advance.....

A: 

Hi Please post the complete code, alternatively you can import the complete worksheet to a Datatable object with the below method and retrieve required columns and rows of DataTable

static public DataTable ExecuteOleDataTable(string sql, string oledbconnectionstring)
   {
       using (OleDbCommand command = new OleDbCommand())
       {
           command.CommandType = CommandType.Text;

           OleDbConnection oledbconnection = new OleDbConnection(oledbconnectionstring);
           command.Connection = new OleDbConnection(oledbconnectionstring); ;
           command.CommandText = sql;

           if (oledbconnection.State == System.Data.ConnectionState.Open)
               oledbconnection.Close();
           oledbconnection.Open();
           OleDbDataAdapter sda = new OleDbDataAdapter(command);
           DataTable datatable = new DataTable();
           sda.Fill(datatable);
           oledbconnection.Close();
           return datatable;
       }
   }
krs