0

everybody; I have this problem in asp.net, I have a page where I insert and modify data, before saving I make a validation if it passes I save the data but if not I raise an exception and show it, the function goes like this;

protected void btnSave_Click(object sender, EventArgs e)
{
try
{
...
if(ValidData())
  //Save
  ...
else
  throw new Exception("Invalid data");
}
catch(Exception ex)
{
  // Javascript alert
  JSLiteral.Text = Utilities.JSAlert(ex.Message);
}
}

The problem is that after I raise the exception and fix the data in the page I click again the save button and it saves but before it shows me again the exception message and its annoying. Even when the data is saved I click again and it shows the message from the exception again.

Do you know the answer for this issue?

0

6 Answers 6

1

If JSLiteral is a server side control and it's using view state. Then you'd need to clear the state of the control, when the save is succesful.

You could disable the viewstate for the control like JSLiteral.EnableViewState =false;

Sign up to request clarification or add additional context in comments.

Comments

1

Are you resetting the value of JSLiteral to empty after you save?

Comments

1

Turn off viewstate for your JSLiteral control.

Comments

0

Is the message saved in the viewstate of the literal?

Explicitly set the literal text to nothing if the data is valid.

Comments

0

My initial guess is that ViewState remembers the error message. Try disabling ViewState on the JSLiteral control.

Comments

0

Throwing & catching an exception in the same function is basically pointless (exception are intended to go across call levels). Everything would probably be better if you wrote it as:

if(ValidData())
{
  //Save
}
 else
{  // Javascript alert
  JSLiteral.Text = Utilities.JSAlert(ex.Message);
}

2 Comments

This is true, but to address the question you also need a JSLiteral.Text = "" in there.
Also when an exception comes from the database it must be catched somewhere and the problem persists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.