Click here to Skip to main content
13,411,015 members (35,808 online)
Rate this:
Please Sign up or sign in to 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());
Posted 15-Jan-13 2:16am
Updated 15-Jan-13 2:25am
sisir patro 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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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. :-)
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.180225.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100