Click here to Skip to main content
15,909,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in this code am getting error any one solve this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("C:\\Documents and Settings\\anush\\Desktop"));
            XmlNodeList bookList = doc.GetElementsByTagName("DocumentElement");
            foreach (XmlNode node in bookList)
            {
                XmlElement bookElement = (XmlElement)node;
                string ctry = bookElement.GetElementsByTagName("DocumentElement")[0].InnerText;
                ddl.Items.Add(ctry);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn("id");
        DataColumn dc1 = new DataColumn("DataBaseName");
        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);
        string ct = ddl.SelectedItem.ToString();
        string dataPath = Server.MapPath("C:\\Documents and Settings\\azhar\\Desktop\\appsettings.xml");
        DataSet dSet = new DataSet();
        dSet.ReadXml(dataPath);
        DataRow[] rows = dSet.Tables[0].Select(" DataBaseName= '" + ct + "'");
        foreach (DataRow dr in rows)
        {
            DataRow myRow = dt.NewRow();
            myRow["id"] = dr["id"];
            myRow["DataBaseName"] = dr["DataBaseName"];
            dt.Rows.Add(myRow);
        }
        grxml.DataSource = dt;
        grxml.DataBind(); 
    }
}
Posted
Updated 24-Feb-12 23:31pm
Comments
Varun Sareen 25-Feb-12 5:31am    
edit for: Added pre tag
anusha 2 25-Feb-12 5:34am    
how can i do that

this is my aspx code.........


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Reading XML</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl" runat="server"> <br />
<asp:TextBox ID="TextBox1" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
<asp:GridView ID="grxml" runat="server">

</form>
</body>
</html>
Aniket Yadav 25-Feb-12 5:32am    
What is the error? Please elaborate.
anusha 2 25-Feb-12 5:36am    
C:/Documents and Settings/anusha/Desktop' is a physical path, but a virtual path was expected. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: 'C:/Documents and Settings/anusha/Desktop' is a physical path, but a virtual path was expected. Source Error: Line 14: { Line 15: XmlDocument doc = new XmlDocument(); Line 16: doc.Load(Server.MapPath("C:\\Documents and Settings\\anusha\\Desktop")); Line 17: XmlNodeList bookList = doc.GetElementsByTagName("DocumentElement"); Line 18: foreach (XmlNode node in bookList)
Varun Sareen 25-Feb-12 5:32am    
what error?

The error is pretty explicit:
"'C:/Documents and Settings/anusha/Desktop' is a physical path, but a virtual path was expected."
C#
doc.Load(Server.MapPath("C:\\Documents and Settings\\anusha\\Desktop"));
uses a physical path - i.e. one that specifies exactly where to look - on the current pc, hard drive C, folder ...
MapPath is used to convert a virtual drive (i.e. one that is relative to the web site) into a physical drive.

Either remove the Server.MapPath call completely, or replace the physical specification with a virtual one. In your case, you would have to remove MapPath, but you really shouldn't assume that you will have a login on the server - if you load this onto a "normal" webhost you almost certainly won't!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 2:50am    
Good catch, a 5.
--SA
if you already know your folder is:
C#
"C:\\Documents and Settings\\anush\\Desktop"
then you do not need to use Server.MapPath.

Just write

C#
doc.Load("C:\\Documents and Settings\\anush\\Desktop");


Hope this will solve your problem.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Feb-12 2:51am    
Same thing; my 5, too.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900