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

LVL 7
jay_eireAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.