Rukmal Weliwatta
Rukmal Weliwatta

Reputation: 1

Uploading a Excel Sheet in C# using Visual Studio

My code is

string PathConn = "Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox10.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";";

OleDbConnection conn = new OleDbConnection(PathConn);

OleDbDataAdapter dbDataAdapter = new OleDbDataAdapter("Select * From [" + textBox1.Text + "$]", conn);

DataTable dt = new DataTable();
dbDataAdapter.Fill(dt);

dataGridView1.DataSource = dt;

I get the error:

System.Data.OleDb.OleDbException: ''$' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long

What am I doing wrong?

Upvotes: 0

Views: 107

Answers (1)

Holly Plyler
Holly Plyler

Reputation: 339

Could it possibly be the "$" in

("Select * From [" + textBox1.Text + "$]", conn);

Try

("Select * From "+ textBox1.Text , conn);

or

("Select * From ["+ textBox1.Text +"]", conn);

which looks like a more correct sql statement to me.

And if I might make a suggestion, try giving your text boxes names that mean more so that your code is more maintainable in the future.

Upvotes: 1

Related Questions