Exception changes textbox every other exception instead of each one

  • Thread starter Thread starter G-Oker
  • Start date Start date
G

G-Oker

Guest
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.

Continue reading...
 
Back
Top