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 :(