excel and c#

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
 
A

Arne Janning

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top