I am getting error:
Connection must be valid and Open c# Winform.
Here is my code, can anyone please help what is wrong in the below code?
public void NameSearch()
{
listBox1.Visible = true;
try
{
String constring = "datasource=localhost;port=3306;Initial Catalog = 'svms'; username = svms; password =svms2016CPU";
string query = "select * from studentinformation where StudLname='" + metroTextBox1.Text + "'";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string Lname = myReader.GetString(myReader.GetOrdinal("StudLname"));
string Fname = myReader.GetString(myReader.GetOrdinal("StudFname"));
string Mname = myReader.GetString(myReader.GetOrdinal("StudMname"));
string nameResult = Lname + ", " + Fname + " " + Mname;
listBox1.Items.Add(nameResult);
}
conDataBase.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}