-1

Working on a school project, i found an interesting code to make a input box for c# here. Im using it to make a find request in a DB. But i got a problem. when i close the message box with cancel or the close button, it execute the catch, while the catch is intended for a connexion problem. How i avoid that?

Edit : With some help i identified the reason of the exception. Input remains empty and cause the exception. How i check now if the message box was closed, to make a condition where i give a value in that case?

Here is the code of the "find" option:

private void TEACHER_BTN_find_Click(object sender, EventArgs e)
{
    string input = "";
    ShowInputDialog(ref input);
    try
    {
        cnx.Open();
        string req1 = "select nom_prof, pre_prof, ville_prof from Professeur where mat_prof ="+input;
        cmd.CommandText = req1;
        OleDbDataReader selection = cmd.ExecuteReader();
        if (selection.Read())
        {
            TEACHER_TXB_reg.Text = input;
            TEACHER_TXB_name.Text = selection[0].ToString();
            TEACHER_TXB_lastn.Text = selection[1].ToString();
            TEACHER_TXB_city.Text = selection[2].ToString();
        }
        else
            MessageBox.Show("Matricule introuvable.");
        cnx.Close();
    }
    catch
    {
        MessageBox.Show("Erreur de connexion.");
    }
}
hnefatl
  • 5,860
  • 2
  • 27
  • 49
Amine Fellous
  • 83
  • 1
  • 8
  • Did you check which exception you're getting? – Lasse V. Karlsen Apr 28 '18 at 16:31
  • @LasseVågsætherKarlsen i get no exception message or anything in visual studio – Amine Fellous Apr 28 '18 at 16:35
  • @AmineFellous remove that useless try/catch and let the exception show itself. If you have a problem, writing out "I have a problem" does not help – Steve Apr 28 '18 at 16:36
  • Do `Catch(Exception Error)` then inside the catch, type `MessageBox.Show(Error.Message)` Then let us know what does it say – SunAwtCanvas Apr 28 '18 at 16:39
  • 3
    1. Your code is open to [SQL injection](http://www.bobby-tables.com)! Don't insert user input directly into your query string, use **parameterized queries** instead. 2. Change that `catch` to a `catch (Exception e)` and show what information `e` contains. 3. I'm sure if you "cancel" the input dialog, `input` does not contain something useful, so you get a db syntax exception. You need to check the if the dialog was cancelled and if so, don't try to execute the query. – René Vogt Apr 28 '18 at 16:40
  • If the code ends up in the catch block, then yes, you **did** get an exception. Since you ignore *which* exception you're getting it's hard to diagnose. – Lasse V. Karlsen Apr 28 '18 at 16:42
  • @RenéVogt i didnt knew about the `catch (Exception e)`. I tried, and it was as you said, `input` remain empty and it's what cause the ecception, but i dont know how to check if the dialog was closed or not. Im still new to c# and coding windows forms. – Amine Fellous Apr 28 '18 at 16:49
  • After the Edit. "Input remains empty": then it is now time to post ShowInputDialog() – H H Apr 28 '18 at 17:48
  • @HenkHolterman `ShowInputSialog()` is a function i found in another thread, and i posted the link. – Amine Fellous Apr 28 '18 at 19:25
  • You did not check the result of that method, if the user cancels it, you shouldn't execute the rest of your code. – Lasse V. Karlsen Apr 28 '18 at 19:57

1 Answers1

2

You can have multiple catch clauses for catching specific or general exception. To catch a general exception, you may

catch (Exception ex)
{
  //handle general exception here.
  MessageBox.Show(ex.Message);
}

To catch a specific exception, in your case, a Database exception, you may

catch (DbException exDb)
{
  //handle your db exception here. It will help you debug as well. You can 
  //log them somewhere for future reference.
}

Hope it helps :)

Abdul Samad
  • 441
  • 2
  • 11