Good day, I want to check my DB connection and here's my code.
private void Button2_Click(object sender, EventArgs e)
{
string mysqlcon = "SERVER=192.XXX.XXX.XXX;PORT=XXXX;DATABASE=XXXX;UID=XXXX;PASSWORD=XXXX;";
MySqlConnection mysqlconnection = new MySqlConnection(mysqlcon);
try
{
mysqlconnection.Open();
label2.ForeColor = System.Drawing.Color.Green;
label2.Text = "Connected";
}
catch (Exception ex)
{
label2.ForeColor = System.Drawing.Color.Red;
label2.Text = ex.Message;
}
finally
{
mysqlconnection.Close();
}
}
now, when I click one's the CHECK button it shows like this.
![User's image](https://learn-attachment.microsoft.com/api/attachments/2f1801bc-969f-4c11-83f0-8aa4991492bf?platform=QnA)
It show's the error message that say. "Object cannot be cast from DBNull to other types."
Now, when I click the check button twice, it shows like this.
![User's image](https://learn-attachment.microsoft.com/api/attachments/e4921c10-283e-4197-947c-92c272b074b2?platform=QnA)
It's shown Connected. I don't know why.
Please help me to fix this problem. Thank you so much.