How to stop a label from displaying on postback if there are errors in asp.net c# web form application?

Hello Experts.

I am working on a simple web form that submits information to a 2012 sql server database.  No duplicate entries are allowed, so I have added a try-catch to the code behind to display the error message.  But I have a label that is set to appear when the page posts back stating that the record was successfully created.  I need this message to be suppressed when there is an error.  Any help would be greatly appreciated.

The following is the code I need suppressed if there's an error:
protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                new_doc_rvw_submitted_lbl.Text = ("Your new document review has been created.");
            }
         }

Open in new window


The following is my try-catch:
try
                {
                    my_cnxn.Open();
                    my_cmd.ExecuteNonQuery();
                }

                catch(SqlException ex)
                {
                    lblMessage.Text = ex.Message;
                    
                }

Open in new window


How can I suppress 'new_doc_rvw_submitted_lbl' if there's an error?

I am currently using Visual Studio 2013

Thanks,
J
ferguson_jeraldAsked:
Who is Participating?
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.

Ryan ChongCommented:
you can set the Visible property of the label control, like:

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == true)
            {
                
            }
         }

Open in new window

try
                {
                    my_cnxn.Open();
                    my_cmd.ExecuteNonQuery();

                    new_doc_rvw_submitted_lbl.Text = ("Your new document review has been created.");
                   new_doc_rvw_submitted_lbl.Visible = true;
                }

                catch(SqlException ex)
                {
                    lblMessage.Text = ex.Message;
                    new_doc_rvw_submitted_lbl.Visible = false;
                }

                                

Open in new window

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
ferguson_jeraldAuthor Commented:
Perfect!  Thank you for such a quick and excellent reply.
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
.NET Programming

From novice to tech pro — start learning today.

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.