So i have the following string in c# that returns a mysql query.
string id = cmd.ExecuteScalar().ToString();
The problem is that if the indput to this string is not valid it crashes the app. This should not crash the app but show this:
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Tried with try and catch with no luck. can someone help?
some of my tries:
try
{
string id = cmd.ExecuteScalar().ToString();
}
catch (Exception ex)
{
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
and
try
{
string id = cmd.ExecuteScalar().ToString();
}
catch (catch (MySqlException))
{
MessageBox.Show("Your key (" + KeyNr + ") was invalid or not active. \nPlease check if it is the correct key or you need to buy another month.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
all gives me:
Error 2 The name 'id' does not exist in the current context
Try this piece of code:
You can even catch it to more specific Exception instead of having generic
Exception.