Changing Textbox.text not working

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

G-Oker

Guest
Hello,

I'm trying to get a button, when pushed, to change the text in a text box, text an SQL connection, then display another message.

So,

1) button pressed

2) textbox message reads "connecting to SQL"

3) SQLconnectionreoutine run

4) textbox message changed accordingly


the "5 sec" message is there for debugging only, and will not be staying.


However, only the last message is displayed.

I have tried SLEEP and a Stopwatch constructor, but the same thing happens.

private void button1_Click(object sender, EventArgs e)
{
UserMsg.Clear();
UserMsg.BackColor = Color.Orange;
UserMsg.ForeColor = Color.Black;
UserMsg.Text = "Connecting to SQL..."; <--- DOSN't DISPLAY



Stopwatch sw = new Stopwatch(); // sw cotructor
sw.Start(); // starts the stopwatch
for (int i = 0; ; i++)
{
if (i % 100000 == 0) // if in 100000th iteration (could be any other large number
// depending on how often you want the time to be checked)
{
sw.Stop(); // stop the time measurement
if (sw.ElapsedMilliseconds > 5000) // check if desired period of time has elapsed
{
break; // if more than 5000 milliseconds have passed, stop looping and return
// to the existing code
}
else
{
sw.Start(); // if less than 5000 milliseconds have elapsed, continue looping
// and resume time measurement
}
}
}

UserMsg.Text = "5 secs are up..."; <--- DOSN't DISPLAY

using (SqlConnection connection = new SqlConnection(@"Data Source=" + SQLServer + ";Integrated Security=False;User ID=sa;Password=password;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;Initial Catalog=" + databasename + ";"))
{
try
{
connection.Open();
UserMsg.BackColor = Color.LimeGreen;
UserMsg.ForeColor = Color.White;
UserMsg.Text = "Connected to SQL...";
chkSQL.Visible = false;
}
catch (SqlException)
{
UserMsg.BackColor = Color.Red;
UserMsg.ForeColor = Color.White;
UserMsg.Text = "..Unable to Connect to SQL Server. close this application, review the Config file and fix.";
System.IO.File.AppendAllText(errorlogpath, "\t" + errormsg);
chkSQL.Visible = true;
return;
}
finally
{
connection.Close();
}
}
}

If I comment out the SQLconnection part, the "5 secs" message appears, but the preceding "connecting" message is never displayed.

If I comment out the SQLConnect part and the stopwatch part (and the "5 secs" message, then the "connecting message is displayed).

Please, can someone tell me the (probably obvious to everyone else) mistake I am making, and what I need to do to rectify?


Many thanks in advance

Continue reading...
 
Back
Top