-5

I have written this code in my form "Load" Event

        MessageBox.Show("Message 1");
        string strConnectionString = @"Data Source=HB-VAIO\SQLEXPRESS;Initial Catalog=DB1;Integrated Security=True;Pooling=False";
        SqlCommand cmdAddPackage = new SqlCommand("AddPackage");
        SqlConnection con = new SqlConnection();
        con.ConnectionString = strConnectionString;

        cmdAddPackage.CommandType = CommandType.StoredProcedure;
        cmdAddPackage.Parameters.Add(new SqlParameter("@GUIDOutput",SqlDbType.UniqueIdentifier)).Direction = ParameterDirection.Output;

        Guid GUI = (Guid) cmdAddPackage.ExecuteScalar();   // Error should be raised but there is no error
        MessageBox.Show(GUI.ToString());
        cmdAddPackage.Connection.Close();
        textBox1.Text = GUI.ToString();
        MessageBox.Show("end");

I know the code is wrong! and it should raise "Cannot open database "DB1" requested by the login. The login failed. Login failed for user 'HB-VAIO\SONY'." in line 8, but when I run the program no error is raised, the first message box pops out but there is no sign of the second and third message box and the form loads successfully.

the wrong things with code are these :

  • My Database name is "DB" but in this code I used "DB1".

  • connection for cmdAddPackage is not set.

When I add same code to button1_Click event, it shows error.

Can You help me why this happens ?

I didn't find any answer for it :(

Hadi Barak
  • 480
  • 1
  • 4
  • 17
  • 8
    Use the debugger. – H H Apr 25 '13 at 13:19
  • Could you please justify why you think the code should fail? – tnw Apr 25 '13 at 13:20
  • You don't even have a try/catch in – Ruan Apr 25 '13 at 13:20
  • It seems your code has been wrapped up in some try ... catch block. Is it? try debugging the code / step through the each line. – Khadim Ali Apr 25 '13 at 13:20
  • 1
    related: [VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows](http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a/4934010#4934010), also: [Why the form load can't catch exception?](http://stackoverflow.com/questions/3209706/why-the-form-load-cant-catch-exception) – sloth Apr 25 '13 at 13:21
  • Just a tought, but perhaps the constructor of `SqlConnection` looks at the config file, while perhaps changing the connectionstring needs a call to `SqlConnection.Open()` – Silvermind Apr 25 '13 at 13:25
  • @tnw as I said the connection property of cmdAddPackage is not set. there is no open connection for cmdAddPackage and database name is wrong – Hadi Barak Apr 25 '13 at 15:57

1 Answers1

0

What UI frameworks do you use? Some .Net UI Frameworks indeed provide you some "Load" events, but also some happen to SILENCE all exceptions that happen during your even-handlers, "to provide better UI Experience to the end user".

You can check easily if any exceptions are thrown in the OUTPUT panel under debugger. If you see any "First chance exception" - then here you have it, your UI Framework is "quiet one".

Add try-catch, or add application.unhandledexception eventhandler, and just try to bear with your quiet UI..

If I recall correctly, the WPF liked to silently skip exceptions, while WinForms tended to crash instantly. But I may be wrong here.

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
  • thank you for your Answer. I'm Using 64 bit windows and as **Dominic Kexel** said in previous comments this problem is related to windows 64 bit. – Hadi Barak Apr 25 '13 at 16:01