Last post Sep 15, 2010 05:01 AM by xandeq
Sep 11, 2010 06:25 AM|xandeq|LINK
I have an Excel file called Products.xls .
I have Columns A and B, with the titles NAME and QUANTITY.
The name of the sheet is SHEET1.
The file has about 40 lines.
How do i show these data on a Gridview or Listview ?
read excel file show gridview
Sep 11, 2010 08:29 AM|sansan|LINK
You can check this link
Referring to the code in the above post, you can bind GridView from the reader as
Try that and see if it works.
Sep 13, 2010 05:10 AM|xandeq|LINK
Im trying like this:
string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;
Data Source=Products.xls;Extended Properties=
DbProviderFactory factory =
DbDataAdapter adapter = factory.CreateDataAdapter();
DbCommand selectCommand = factory.CreateCommand();
selectCommand.CommandText = "SELECT NAME,QUANTITY FROM [Sheet1$]";
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
selectCommand.Connection = connection;
adapter.SelectCommand = selectCommand;
DataSet dspr = new DataSet();
GridView1.DataSource = dspr;
ANd it returns me an error on the adapter.Fill(cities,"NAME,QUANTITY");
Is anything wrong with my code ?
Sep 14, 2010 07:20 PM|sansan|LINK
That line should work fine. Second Parameter is the dataTable Name. you can leave that blank as adapter.Fill(dspr) and the first Table Name will be Table_0 or you can access the table by using index adapter.Tables.
Make it adapter.Fill(dspr,"Cities").
Sep 15, 2010 05:01 AM|xandeq|LINK
the error was on the connection string Data Source path.
i was writing only file.xls, where i should write the whole path C:\......\file.xls
And i had to place adapter.Fill(cities,"NAME,QUANTITY");