Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I don't know what's going on with visual studio 2012? when I run the following code in Visual Studio 2010, it works fine as expected. but when I run the same code in Visual Studio 2012, neither it throws exception nor it notify me about the exception. can some one tell me the matter?

string connectionString = "Data Source=xyz;Initial Catalog=abc;User ID=sa; Password=123; Integrated Security=true ";
           SqlConnection sqlConnection = new SqlConnection(connectionString);
               SqlCommand sqlCommand = new SqlCommand("SELECT COUNT(*) FROM tblProduct");
               sqlCommand.Connection = sqlConnection;

               int RecordCount = Convert.ToInt32(sqlCommand.ExecuteScalar());
           catch (Exception ex)
             MessageBox.Show(   ex.Message.ToString());
Updated 15-Jan-13 1:25am
[no name] 15-Jan-13 10:44am    
Can you tell me what does your code do here?
Faisalabadians 15-Jan-13 10:57am    
when control enters in "try" body and reads "sqlConnection.Open()" then there hangs for some time and after that form is shown, mean remaining code after the line "sqlConnection.Open();" is not executed.
Sergey Alexandrovich Kryukov 15-Jan-13 11:57am    
It has nothing to do with Visual Studio whatsoever.

Maybe, this helps: Set in VisualStudio in the Debug --> Exceptions --> .Net Exception stop on throw.
Then debug the code again: the debugger will stop at the throw-location.
Share this answer
Your connection is timing out.. or rather isn't timing out... and that is why the app is hanging. You are opening the connection to the server on the UI thread and it isn't able to connect to your datasource so it is making your application hang while it is trying to connect. I think the default timeout is 3 minutes... if you wait that long it should, finally, throw an exception. If you aren't that patient, you can set the ConnectionTimeout property of the sqlConnection object before you call the Open method, then the method will timeout sooner if it can't connect to the datasource.

If connectivity to your datasource will be intermittent, then you should consider moving the connection management to a non-UI thread so the delays in opening a SQL connection are hidden from the user.

As to why it can't connect, that is another issue and it is highly dependent on what you are connecting to. Unfortunately, 'xyz' doesn't tell us much so I can't help you there. :-)
Share this answer
Faisalabadians 16-Jan-13 2:20am    
I used same code in visual studio 2010 with .Net framework 4 and got no error and connection is established. but visual studio 2012 is not working.
Jason Gleim 16-Jan-13 11:13am    
Is SQL Server running on your development machine or on another machine? If it is on your machine you aren't doing something silly like trying to run both versions (2010 and 2012) at once? If it is on a remote machine, have you tried using WireShark to look at the traffic between the two systems?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900