469,917 Members | 1,489 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

import excel file

i want to import excel file to gridview but there was error(Failed to map this path "/xmltest.xml") and i couldn't handle it my code:
Expand|Select|Wrap|Line Numbers
  1. public void fillGrid()
  2.     {
  3.         DataSet ds = new DataSet();
  4.         ds.ReadXml(Server.MapPath("../xmltest.xml"));
  5.         GridView1.DataSource = ds;
  6.         GridView1.DataBind();
  7.     }
  8.     protected void Page_Load(object sender, EventArgs e)
  9.     {
  10.         if (!IsPostBack)
  11.         {
  12.             fillGrid();
  13.         }
  14.     }
  16.     protected void btImport_Click(object sender, EventArgs e)
  17.     {
  18.         string conn = ("Provider=Microsoft.Jet.OLEDB.4.0;" + ("Data Source=C:\\WebSqlEditor\\Test.xls;" + "Extended Properties=\"Excel 8.0;\"")); 
  19.         string SSQL = "SELECT name , dept, salary  from [sheet1$]";
  20.         OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn); 
  21.         DataSet ds = new DataSet();
  22.         oleDA.TableMappings.Add("Table","ExcelTest"); 
  23.         oleDA.Fill(ds);
  24.         GridView1.DataSource = ds.Tables[0].DefaultView; 
  25.         GridView1.DataBind();
  26.     }
i need any idea to fix this error,i tried more to fix it but i cann't
Mar 20 '09 #1
4 2511
7,872 Expert 4TB
Does the xmltest.xml file exist in the parent directory of wherever you are?
You should try outputing the result of Server.Mappath, to see what its returning (if anything at all)

Also I think that if you try to go to directory higher up then the "root" directory for your webapplication, it will fail
Mar 20 '09 #2
i made the source of excel file in c:websqleditor
in this folder i made excel file called(test) as i wrote in code
note if i changed the path the same error appear
i wonder if i need any thing to map path
Mar 20 '09 #3
7,872 Expert 4TB
Well by default your webpage would be in like c:\inetpub\wwwroot\
So running Server.MapPath("../xmltest.xml"); Would produce c:\inetpub\xmltext.xml
Which would be above the "root" of your web application, and thus the server.mappath call would error because you are trying to access a file outside of its dominion
Mar 20 '09 #4
at first my application wasn't in the root c:\Inetpub\wwwroot\.....
i put it in this root and i changed the path in my code to be suitable for path but the error still appear
Mar 20 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Johnny Meredith | last post: by
10 posts views Thread by Niklas | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.