none
Exception changes textbox every other exception instead of each one RRS feed

  • Question

  • I have some code that looks for a change in an XML, and then writes the changes to a SQL DB.

    When it has a connection to the DB, it changes a text box's background, foreground and text.

    If an exception is CATCHed with s specific line code number, I then ask it to write the data to a file instead and change a textbox's background colour, foreground colour and text.

    whats is happening is when CATCH coding is working fine, except for the first 3 lines (which change the textbox properties).

    this only seems to happen to every other exception that is caught.

    private void OnChanged(object source, FileSystemEventArgs e) { TextBox.CheckForIllegalCrossThreadCalls = false; //find a better solution try { //// SORT THESE 3 LINES OUT. ONLY dISPLAY IF NO EXCEPTION - seems to be alternating between this and the exception on each exception UserMsg.BackColor = Color.LimeGreen; UserMsg.ForeColor = Color.White; UserMsg.Text = "Connected to SQL..."; XmlDocument xmldoc = new XmlDocument(); Stream myStream = null; using (myStream) { xmldoc.Load(FldPath + "\\Collector.xml"); .... some other code con.Open(); // LINE 742 cmd.ExecuteNonQuery(); var returnedresult = returnParameter.Value; int returnedresultint = Convert.ToInt16(returnParameter.Value); if (returnedresultint != 0) // if the return value is 1 or Null, then process failed { System.IO.File.AppendAllText(errorlogpath, "\t" + errormsg3); } else { tbOutput.AppendText(">>> data It has been saved to SQL." + Environment.NewLine); } } } catch (Exception ex) { if (this.tbOutput.InvokeRequired) { // Get stack trace for the exception with source file information System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true); // Get the stack frame var stackFrame = trace.GetFrame(trace.FrameCount - 1); // Get the line number from the above stack frame, to a string string lineNumber = stackFrame.GetFileLineNumber().ToString(); // if exception is on line number 742 (con.Open();), then connection to HART server lost if (lineNumber == "742") { UserMsg.BackColor = Color.Orange; // CHANGES EVERY OTHER TIME THIS CODE IS RUN THOUGH UserMsg.ForeColor = Color.Black; // UserMsg.Text = "Lost connection to SQL..."; // UserMsg.Visible = true; // tbOutput.AppendText(" *** UNABLE TO WRITE DATA to the SQL. Written to the file instead." + Environment.NewLine);

    // above line writes everytime (whichi is correct) } } } }


    What I need to happen is the UserMsg textbox to change its background, foreground and text every time the CATCH code is run (at the moment, it's doing it every other time).

    I've tried making UserMsg.Focus and making it Visable = false, and visable = true, but it is like it just skips the 

    What have I done that cause this to happen every other time, instead of every time the exception on line 742 is encountered?

    Thanks in advance for your time.

    Thursday, August 15, 2019 5:15 PM

Answers

  • Greetings G-Oker.

    I suspect the code is coming back into the OnChanged method so fast that you don't notice the colours changing before they are changed back (at the top of the 'try' block). You do notice the tbOutput change, on the other hand, because you are appending text which doesn't go away the next time through.

    And by the way, you really shouldn't use try/catch to control program flow like that. The 'catch' part should be for exceptions you don't expect,  while events you do expect should be handled without causing an exception. Putting it another way, if you are getting exceptions, it means you are doing something that needs to be fixed (that's what an exception means).

    • Marked as answer by G-Oker Monday, August 19, 2019 9:27 AM
    Thursday, August 15, 2019 11:36 PM

All replies

  • Greetings G-Oker.

    I suspect the code is coming back into the OnChanged method so fast that you don't notice the colours changing before they are changed back (at the top of the 'try' block). You do notice the tbOutput change, on the other hand, because you are appending text which doesn't go away the next time through.

    And by the way, you really shouldn't use try/catch to control program flow like that. The 'catch' part should be for exceptions you don't expect,  while events you do expect should be handled without causing an exception. Putting it another way, if you are getting exceptions, it means you are doing something that needs to be fixed (that's what an exception means).

    • Marked as answer by G-Oker Monday, August 19, 2019 9:27 AM
    Thursday, August 15, 2019 11:36 PM
  • Hi G-Oker,

    Thank you for posting here.

    >>What have I done that cause this to happen every other time, instead of every time the exception on line 742 is encountered?

    I try to reproduce your problem and figure out why this will happen. 

    If you operate on the file multiple times, the OnChanged event will be fired multiple times, which will cause the color of the textbox to change each time it encounters the exception.

    Therefore, please change file only one time to see if it still exists the problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 16, 2019 3:14 AM
    Moderator
  • In order to detect database problems, you can use a specialised try-catch:

    . . .

    try

    {

       con.Open();

       cmd.ExecuteNonQuery();

    }

    catch( Exception exc )

    {

       change the colours . . .

       write to file . . .

    }

    . . .

     

    By the way, InvokeRequired means that you cannot access the controls directly.

    Friday, August 16, 2019 5:32 AM
  • Thank you everyone for your helpful replies and help here. I have amaned my codes, and am now using a differnet method.

    thanks again

    G-Oker

    Monday, August 19, 2019 9:29 AM