By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,836 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,836 IT Pros & Developers. It's quick & easy.

import excel file

P: 49
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.     }
  15.  
  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
thanks
Mar 20 '09 #1
Share this Question
Share on Google+
4 Replies


Plater
Expert 5K+
P: 7,872
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

P: 49
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

Plater
Expert 5K+
P: 7,872
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

P: 49
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.