• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

Replace / in file name with _ display friendly error message

Hi,
I have a ASP.NET C# web-page that has two form fields called:
WebAddress
WebAddressContents

When user clicks the SAVE button the contents of the form are saved to a TXT file, with the WebAddress as the TXT file name and also as the first line in the body of the TXT file along with the contents of WebAddressContents in the body of the TXT file.

A problem I have is when the web address has a slash in it example:
http://www.google.com/apps/intl/en/terms/user_features.html the page crashes,

How can I replace the / in the TXT file name with an _ underscore instead? Id still like to keep the original address intact with the slashes as the first line of the document

If my page decides to crash can i display a more friendly error message on the page as in "sorry please try again"

here is my 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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
        <asp:TextBox ID="WebAddress" runat="server" style="width:300px; Height:20px;" value="http://www.google.com/apps/intl/en/terms/user_features.html"></asp:TextBox><br /><br />
        <asp:TextBox ID="WebAddressContents" runat="server" style="width:300px; Height:200px;" value="Google Apps Core Services summary (if Google Apps for Business – also known as Google Apps Premier Edition – or Google Apps for Government is selected in Order Form or otherwise purchased or if Customer is receiving Google Apps for Education – also known as Google Apps Education Edition)

    * 'Gmail' is a web-based e-mail service that allows an organization to run its e-mail system using Google’s systems. It provides the capability to access an End User's inbox from a supported web browser, read mail, compose, reply to, and forward mail, search mail, and manage mail through labels. As of the Effective Date, Gmail provides 25GB of disk space per End User for Google Apps for Business (and Google Apps for Government) customers and 7GB of disk space per End User for Google Apps for Education customers). "></asp:TextBox>
    </div>
    <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
<asp:Label id="lblSuccess" runat="server" />

    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "") + ".txt");
        sw.WriteLine(WebAddress.Text);
        sw.WriteLine(WebAddressContents.Text);
        sw.Close();
        lblSuccess.Text = "The file has been saved successfully.";
    }

}

Open in new window

0
jay_eire
Asked:
jay_eire
  • 3
  • 3
2 Solutions
 
Roman GhermanSenior Software EngineerCommented:
Try this for friendly message:
protected void btnSave_Click(object sender, EventArgs e)
    {
       try
       {
               StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "") + ".txt");
               sw.WriteLine(WebAddress.Text);
               sw.WriteLine(WebAddressContents.Text);
               sw.Close();
               lblSuccess.Text = "The file has been saved successfully.";
       }
       catch()
        {lblSuccess.Text = "Sorry Please Try again.";}
    }

Open in new window

0
 
John ClaesSenior .Net Consultant & Technical AnalistCommented:

try the folowing ( I'll add comments why)


protected void btnSave_Click(object sender, EventArgs e)
    {
       try
       {
// The extra Replace is used to ensure no / is in the filename.
               StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").replace("/","_") + ".txt");
               sw.WriteLine(WebAddress.Text);
               sw.WriteLine(WebAddressContents.Text);
               sw.Close();
               lblSuccess.Text = "The file has been saved successfully.";
       }
       catch() // Here I can offer you 2 options : I choose option 1 : but option 2 is used by some of my clients
      {
          // This will fill a label with your text and everything else stays as is so the user can retry
          // Ensure by css that this ErrorLabel is clear and takes focus from the user
          ErrorLabel.Text = "Sorry Please Try again.";}

          // This will Make a Fully clean Page with only the message
          Response.Write ("Sorry Please Try again.<br />")

      }
    }
0
 
jay_eireAuthor Commented:
Thanks for the replies.

@poor_beggar,

I got a build error on catch()
so I change it to.
catch // Here I can offer you 2 options : I choose option 1 : but option 2 is used by some of my clients

Im not getting an error on line
 StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").replace("/","_") + ".txt");

it says .replace is underlined in red and build error is
Error      1      'string' does not contain a definition for 'replace'      C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite5\Default2.aspx.cs      47      105      C:\...\WebSite5\

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
Roman GhermanSenior Software EngineerCommented:
StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").Replace("/","_") + ".txt");
0
 
jay_eireAuthor Commented:
below is the code I am using, the error message is coming back for line 47 'string' does not contain a definition for 'replace'      on the second .replace               StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").replace("/","_") + ".txt");


protected void btnSave_Click(object sender, EventArgs e)
    {
       try
       {
// The extra Replace is used to ensure no / is in the filename.
               StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").replace("/","_") + ".txt");
               sw.WriteLine(WebAddress.Text);
               sw.WriteLine(WebAddressContents.Text);
               sw.Close();
               lblSuccess.Text = "The file has been saved successfully.";
       }
       catch // Here I can offer you 2 options : I choose option 1 : but option 2 is used by some of my clients
      {
          // This will fill a label with your text and everything else stays as is so the user can retry
          // Ensure by css that this ErrorLabel is clear and takes focus from the user
          ErrorLabel.Text = "Sorry Please Try again.";}


      }
0
 
jay_eireAuthor Commented:
@roma1123  thank you, that updated statement worked. :)

I will split the points thanks guys.
0
 
Roman GhermanSenior Software EngineerCommented:
you need to use Replace instead of replace.

protected void btnSave_Click(object sender, EventArgs e)
    {
       try
       {
// The extra Replace is used to ensure no / is in the filename.
               StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "").Replace("/","_") + ".txt");
               sw.WriteLine(WebAddress.Text);
               sw.WriteLine(WebAddressContents.Text);
               sw.Close();
               lblSuccess.Text = "The file has been saved successfully.";
       }
       catch // Here I can offer you 2 options : I choose option 1 : but option 2 is used by some of my clients
      {
          // This will fill a label with your text and everything else stays as is so the user can retry
          // Ensure by css that this ErrorLabel is clear and takes focus from the user
          ErrorLabel.Text = "Sorry Please Try again.";}


      }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now