excel and c#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the following code to read an excel file and display the data on a data grid Code:

string strconn;
strconn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\\\test.xls;" +
"Extended Properties=Excel 8.0;";


OleDbConnection oConn = new OleDbConnection ();
oConn.ConnectionString = strconn;
oConn.Open ();

//create new OledbCommand to return data from worksheet
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", oConn);

DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables ;
dataGrid1.DataBindings ();
but when I run it, it gives me an error message - f:\c#\Entitlements Viewer\Form1.cs(306): 'System.Windows.Forms.Control.DataBindings' denotes a 'property' where a 'method' was expected
 
freddy said:
I am using the following code to read an excel file and display the data on a data grid Code:

string strconn;
strconn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\\\test.xls;" +
"Extended Properties=Excel 8.0;";


OleDbConnection oConn = new OleDbConnection ();
oConn.ConnectionString = strconn;
oConn.Open ();

//create new OledbCommand to return data from worksheet
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", oConn);

DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables ;
dataGrid1.DataBindings ();
but when I run it, it gives me an error message - f:\c#\Entitlements Viewer\Form1.cs(306): 'System.Windows.Forms.Control.DataBindings' denotes a 'property' where a 'method' was expected

Hi freddy,

simply remove the last line and set
dataGrid1.DataSource = ds.Tables[0];
and it will work.

private void button1_Click(object sender, System.EventArgs e)
{
string strconn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\\\test2.xls;" +
"Extended Properties=Excel 8.0;";

OleDbConnection oConn = new OleDbConnection ();
oConn.ConnectionString = strconn;
oConn.Open ();

//create new OledbCommand to return data from worksheet
OleDbDataAdapter da =
new OleDbDataAdapter("SELECT * FROM [test$]", oConn);

DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0] ;
//dataGrid1.DataBindings ();
}

Cheers

Arne Janning
 
it worked
thanks


Arne Janning said:
freddy said:
I am using the following code to read an excel file and display the data on a data grid Code:

string strconn;
strconn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\\\test.xls;" +
"Extended Properties=Excel 8.0;";


OleDbConnection oConn = new OleDbConnection ();
oConn.ConnectionString = strconn;
oConn.Open ();

//create new OledbCommand to return data from worksheet
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", oConn);

DataSet ds = new DataSet ();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables ;
dataGrid1.DataBindings ();
but when I run it, it gives me an error message - f:\c#\Entitlements Viewer\Form1.cs(306): 'System.Windows.Forms.Control.DataBindings' denotes a 'property' where a 'method' was expected

Hi freddy,

simply remove the last line and set
dataGrid1.DataSource = ds.Tables[0];
and it will work.

private void button1_Click(object sender, System.EventArgs e)
{
string strconn = "Provider = Microsoft.Jet.OLEDB.4.0;" +
"Data Source=c:\\\\test2.xls;" +
"Extended Properties=Excel 8.0;";

OleDbConnection oConn = new OleDbConnection ();
oConn.ConnectionString = strconn;
oConn.Open ();

//create new OledbCommand to return data from worksheet
OleDbDataAdapter da =
new OleDbDataAdapter("SELECT * FROM [test$]", oConn);

DataSet ds = new DataSet();
da.Fill(ds);
dataGrid1.DataSource = ds.Tables[0] ;
//dataGrid1.DataBindings ();
}

Cheers

Arne Janning
 
Back
Top