zahra72
zahra72

Reputation: 21

How can i define "DisplayErrorMessage" in this case?

This is a code that I wrote for my open button ... but I have error on "DisplayErrorMessage" part. What should I write instead? or how can I define it in order not to have the error again.

protected void btnOpen_Click(object sender, EventArgs e)
{
    txtFileName.Text = txtFileName.Text.Trim();
    if (txtFileName.Text == string.Empty)
    {
        string strErrorMessage = "you did Not specify file for opening!";
        DisplayErrorMessage(strErrorMessage);
    }

    string strFileName = txtFileName.Text;
    string strRootRelativePath = "~/app_data/pageContent";
    string strRootRelativePathName =
    string.Format("{0}/{1}", strRootRelativePath, strFileName);
    string strPathName = Server.MapPath(strRootRelativePathName);

    System.IO.StreamReader ostreamReader = null;

    try
    {
        ostreamReader = new System.IO.StreamReader(strPathName, System.Text.Encoding.UTF8);
        litPageMessages.Text = ostreamReader.ReadToEnd();
    }
    catch (Exception ex)
    {
        litPageMessages.Text = ex.Message;
    }
    finally
    {
        if (ostreamReader != null)
        {
            ostreamReader.Dispose();
            ostreamReader= null;
        }
    }
}

Upvotes: 1

Views: 157

Answers (3)

zed
zed

Reputation: 2338

If you want to alert your error message in the browser, you could do the following.

Add a class file in your App_Code folder, say Helpers.cs
Then, open it and add the following code:

public class Helpers
{
    public static void DisplayErrorMessage(Page page, string msg)
    {
        string script = "<script>alert('" + msg + "');</script>";

        if (!page.ClientScript.IsStartupScriptRegistered("MyAlertMsgHandler"))
            page.ClientScript.RegisterStartupScript(page.GetType(), "MyAlertMsgHandler", script);
    }
}

Lately, call this method from your code behind like this:

Helpers.DisplayErrorMessage(this.Page, "Error message details.");

Upvotes: 1

Nalaka
Nalaka

Reputation: 1185

Try This...

void  DisplayErrorMessage(string msg)
    {
        string script = "<script>alert('" + msg + "');</script>";
        if (!Page.IsStartupScriptRegistered("myErrorScript"))
        {
            Page.ClientScript.RegisterStartupScript("myErrorScript", script);
        }
    }

Upvotes: 0

user3016120
user3016120

Reputation: 26

Either create a function which takes message in the parameter and use MessageBox.Show() method to Display the error message.

or

Just Call MessageBox.Show( this, strErrorMessage ) instead of DisplayErrorMessage(strErrorMessage);

Upvotes: 0

Related Questions