473,574 Members | 3,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET 2.0 Label with C# and Textfile html code in Visual Web Developer Express 2005

80 New Member
Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text file which includes some html code to populate html formatted content as the text of an asp:label (<asp:label>). The reading of the text file itself goes just fine ;however, this only occurs when I use an absolute file path which will not work of course once I upload the website to a hosting service. The text file is in the same directory as both the .aspx file and the codebehind the page file in the Website directory in the Visual Studio 2005 directory structure located in "My Documents". Here is the error message that I receive when I attempt to create load the page without an absolute file path to the text file in the StreamReader object:

Expand|Select|Wrap|Line Numbers
  1.  Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
  2. 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.
  3.  
  4. Exception Details: System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.
  5.  
  6. Source Error:
  7.  
  8. Line 17:     protected void Page_Load(object sender, EventArgs e)
  9. Line 18:     {
  10. Line 19:         StreamReader cr = new StreamReader("Default4.txt");
  11. Line 20:         string contents = null;
  12. Line 21:         contents = cr.ReadToEnd();
  13.  
  14.  
  15. Source File: c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs    Line: 19
  16.  
  17. Stack Trace:
  18.  
  19. [FileNotFoundException: Could not find file 'C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\Default4.txt'.]
  20.    System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +1971213
  21.    System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) +998
  22.    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) +115
  23.    System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) +85
  24.    System.IO.StreamReader..ctor(String path) +112
  25.    _Default.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\Main\My Documents\Visual Studio 2005\WebSites\WebSite2\Default4.aspx.cs:19
  26.    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +13
  27.    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +45
  28.    System.Web.UI.Control.OnLoad(EventArgs e) +80
  29.    System.Web.UI.Control.LoadRecursive() +49
  30.    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3745
  31.  
  32.  
  33. Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832
Here is the code for the C# code behind the page file:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11.  
  12. /// <summary>
  13. /// Summary description for Default4
  14. /// </summary>
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         StreamReader cr = new StreamReader("Default4.txt");
  20.         string contents = null;
  21.         contents = cr.ReadToEnd();
  22.         Label1.Text = contents;
  23.         cr.Close();
  24.         //Label1.Text = "<p align=center>Hello World</p>";
  25.     }
  26.  
  27. }
  28.  
Here is the Code to the .aspx file:

Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/Default4.aspx.cs" Inherits="_Default"%>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <script runat="server">
  6.  
  7. </script>
  8.  
  9. <html xmlns="http://www.w3.org/1999/xhtml" >
  10. <head runat="server">
  11. <style type="text/css">
  12.         .staticMenuItem
  13.         {
  14.             color:White;
  15.  
  16.             font-weight: bold;
  17.             text-align: center;
  18.  
  19.             /*border:solid 1px black;
  20.             padding:2px 4px;*/
  21.         }
  22.         .menuHover
  23.         {
  24.             color:#ffcc00;
  25.             background-color:#ff3300;
  26.         }
  27.         .dmenuHover
  28.         {
  29.             color:blue;
  30.             background-color:#ff9900;
  31.         }
  32.         .dynamicMenuItem
  33.         {
  34.             color:white;
  35.             font-weight: bold;
  36.             text-align: center;
  37.             /*background-color:#FF6600;*/
  38.             padding:2px 4px;       
  39.         }
  40.         .dynamicMenu
  41.         {
  42.             /*the background color for the dynamic menus both "popout" and non is covered by the
  43.             DynamicMenuStyle-BackColor="#FF6600" in the <asp:Menu> tag*/
  44.             /*background-color: #ffcc00;*/
  45.             /*background-color: green;*/ 
  46.             /*font-weight: bold;
  47.             text-align: center;*/           
  48.             /*border:Solid 1px black;
  49.             filter:progid:DXImageTransform.Microsoft.dropshadow(OffX=5, OffY=5, Color='gray', Positive='true')" ;*/
  50.         }
  51.     </style>
  52.     <title>Untitled Page</title>
  53. </head>
  54. <body>
  55.     <form id="form1" runat="server">
  56.     <div>
  57.         &nbsp;
  58.         <asp:Menu ID="Menu1" runat="server" BackColor="#990000" DynamicMenuStyle-BackColor="#333333" DataSourceID="SiteMapDataSource1" StaticDisplayLevels="2"
  59.         StaticMenuItemStyle-CssClass="staticMenuItem"
  60.         StaticHoverStyle-CssClass="menuHover"
  61.         DynamicHoverStyle-CssClass="menuHover"
  62.         DynamicMenuItemStyle-CssClass="dynamicMenuItem"
  63.         DynamicMenuStyle-CssClass="dynamicMenu">
  64.             <StaticMenuItemStyle ItemSpacing="20px" />
  65.             <DynamicMenuItemStyle ItemSpacing="0px" />
  66.         </asp:Menu>
  67.         <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>&nbsp;
  68.         <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
  69.  
  70.     </div>
  71.     </form>
  72. </body>
  73. </html>
  74.  
Here is the code located in the text file named Default4.txt:
Expand|Select|Wrap|Line Numbers
  1. <br /><p align=right>Hello There World!!</p>
And finally here is the file path that I must use in the StreamReader object in order to avoid generating an error:

Expand|Select|Wrap|Line Numbers
  1. C:\\Documents and Settings\\Main\\My Documents\\Visual Studio 2005\\WebSites\\WebSite2\\
Is what is going on unfixable? Also could someone experienced with uploading Visual Web Developer 2005 generated sites give me a few pointers on deploying them? Any help here would be greatly appreciated.
Oct 16 '07 #1
1 2877
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
While assigning the file name, you have to provide it with the path of the file too.
If you do not, then it will map it from the default path.
What you need to do is to map it from the servers path, so that it will navigate from the application folder.
User server.Mappath to get the desired path. and then supply it to the stream reader.

cheers
Oct 16 '07 #2

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

Similar topics

3
1864
by: clintonG | last post by:
Visual C# 2005 Express Edition is a simple, lightweight, integrated development environment designed for beginning programmers and non-professional developers interested in building Windows Forms, class libraries, and console-based applications. Visual C# 2005 Express Edition includes many of the same productivity features found in Visual...
5
2566
by: Patrick Olurotimi Ige | last post by:
Hi, I have VStudio.Net 2003 installed but can i install Visual Web Developer also on the same PC. My current .Net Frameork version is 1.1. Will the Visual Web Developer install ASP.NET 2.0? And if i do will it affect my other applications? I want ot build ASP.NET 2.0 apps!!!
1
1829
by: pbishop | last post by:
Hi, I'm fairly new to ASP.Net. I have Visual Web Developer 2005 Express. Except for the default.aspx, whenever I create a new Web form, no code behind (aspx.vb) page is created. The same thing is happening for master pages. I have exited Visual Web Developer 2005 Express and restarted it, and I have rebooted the PC, but the problem...
12
2244
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation errors show up in your error-list, you can disable this functionality by selecting the Tools->Options menu item in VS or Visual Web Developer. ...
2
3420
by: Progman | last post by:
I have Visual Studio 2005 Standard edition. Is ti the same thing as the Express edition or Standard is more?
3
1380
by: barry | last post by:
I had Visual Web Developer Express 2005 and Visual Studio 2005 installed on my computer. Realizing that I did not need Visual Web Developer Express 2005 since I had VS 2005 I removed it. My question could this be a possible reason that the ASP.NET configuration tool is not working in VS 2005 when I am doing web development? What happens is...
21
1581
by: Goofy | last post by:
I see that the code behind is now a partial class. So where is the code associated with the server controls declaration etc ? Is this generated on the fly now, or am I missing something ? -- Goofy
5
2210
by: cj | last post by:
I'm getting started writing WEB program in VB2005. I'm been doing windows apps to date. A friend told me to get Visual Web Developer Express Edition. But I have Visual Studio Pro 2005 and it says I have Visual Web Developer in it. I'm just not sure what that is and how to start it. Any advise?
2
2229
by: cpchan | last post by:
Hello, Can I create a Web-based application (not a Web site) with Visual C# 2005 Express ? Or I have to use Visual Web Developer 2005 Express Edition, choose ASP.NET and then choose C# ? Are there any web-sites give me step-by-step Visual C# 2005 Express Web-based application development ?
0
7841
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8270
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6509
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.