read XLS (Excel) file and fill a gridviewhttp://forums.asp.net/t/1799184.aspx/1?read+XLS+Excel+file+and+fill+a+gridviewWed, 02 May 2012 11:43:29 -040017991844961484http://forums.asp.net/p/1799184/4961484.aspx/1?read+XLS+Excel+file+and+fill+a+gridviewread XLS (Excel) file and fill a gridview <p>Hi all,</p> <p>I have this code:</p> <pre class="prettyprint">string connectionString = @&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=provafile.xls;Extended Properties=&quot;&quot;Excel 8.0;HDR=YES;&quot;&quot;&quot;; string query = &quot;Select * from [Sheet1$]&quot;; DbProviderFactory factory = DbProviderFactories.GetFactory(&quot;System.Data.OleDb&quot;); DbConnection connection = factory.CreateConnection(); connection.ConnectionString = connectionString; DbCommand selectCommand = factory.CreateCommand(); selectCommand.CommandText = query; selectCommand.Connection = connection; DbDataAdapter adapter = factory.CreateDataAdapter(); adapter.SelectCommand = selectCommand; DataTable dt = new DataTable(); adapter.Fill(dt); Session[&quot;Dtable&quot;] = dt; GridView1.DataSource = dt; GridView1.DataBind(); Response.Write(dt.ToString());</pre> <p>that read provafile.xls sheet1 and fill a datatable used as datasource for a gridview.</p> <p>The problem is that the page is empty and I got no errors, I don't find errors in code.. so, where am I wrong?</p> <p>The second question is: how can I build a parametric connection string with the filename coming from a variable?</p> <p>Thanks a lot in advance<br> <br> </p> 2012-05-02T10:16:55-04:004961551http://forums.asp.net/p/1799184/4961551.aspx/1?Re+read+XLS+Excel+file+and+fill+a+gridviewRe: read XLS (Excel) file and fill a gridview <p>ok I've solved the gridview issue.</p> <p>Now I have the second thing to resolve: parametric data source in the connection string.</p> <p>I see that the path does not point to the root of the folder where are my files BUT to C:\Programmi\File comuni\Microsoft Shared\DevServer\10.0\provafile.xls</p> <p>How can I modify the path to something like ~/xls/provafile.xls?</p> <p>Thanks in advance</p> <p></p> 2012-05-02T10:42:11-04:004961558http://forums.asp.net/p/1799184/4961558.aspx/1?Re+read+XLS+Excel+file+and+fill+a+gridviewRe: read XLS (Excel) file and fill a gridview <p></p> <p>hi</p> <p>can you try it once</p> <p>Server.MapPath(&quot;~/xls/provafile.xls&quot;);</p> <p></p> <p>thanks</p> 2012-05-02T10:44:16-04:004961740http://forums.asp.net/p/1799184/4961740.aspx/1?Re+read+XLS+Excel+file+and+fill+a+gridviewRe: read XLS (Excel) file and fill a gridview <p>thanks, but I have to write the remaining string with single quotes as follows:</p> <p>string connectionString = @&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; &#43; Server.MapPath(&quot;~/xls/provafile.xls&quot;) &#43; &quot;;Extended Properties='Excel 8.0;HDR=YES;'&quot;;</p> 2012-05-02T11:43:29-04:00