my objective is to make my button flexible depending on what is currently the value of my combo box, but the problem is when i run my program on that particular event it freezes, is there something wrong with my syntax or my computer is just slow?
private void cmbOperation_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = (string)cmbOperation.SelectedItem;
while (selected == "ADD")
{
txtID.ReadOnly = true;
txtLName.ReadOnly = false;
txtFName.ReadOnly = false;
txtMI.ReadOnly = false;
txtGender.ReadOnly = false;
txtAge.ReadOnly = false;
txtContact.ReadOnly = false;
btnOperate.Text = "ADD CLIENT";
}
}
private void btnOperation_Clicked(object sender, EventArgs e)
{
if (cmbOperation.SelectedItem.Equals("ADD"))
{
string constring = "datasource=localhost;port3306;username=root";
string Query = "insert into mybusiness.client_list (LastName,FirstName,MI,Gender,Age,Contact) values('" + this.txtLName.Text + "','" + this.txtFName.Text + "','" + this.txtMI.Text + "','" + this.txtGender.Text + "','" + this.txtAge.Text + "','" + txtContact.Text + "' ;";
MySqlConnection conDB = new MySqlConnection(constring);
MySqlCommand cmDB = new MySqlCommand(Query, conDB);
MySqlDataReader myReader;
try
{
conDB.Open();
myReader = cmDB.ExecuteReader();
MessageBox.Show("Client Information has been added to the list");
while(myReader.Read())
{
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
You're not changing the condition for your while
loop - so if it's ever true, it will always be true:
string selected = (string)cmbOperation.SelectedItem;
while (selected == "ADD")
{
// Code that doesn't change the value of selected
}
There are other significant problems with your code, however:
ExecuteReader
to perform an insert operation. Use ExecuteNonQuery
instead - ExecuteReader
is designed for queries, and your code isn't querying anything.using
statements for the connection and command, so they are closed automatically when execution leaves the scope of the using
statement - currently your connection will hang around until it is finalized and garbage collected; this can lead to hangs.See more on this question at Stackoverflow