Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VisualStudio2012
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);
            try
            {
                sqlConnection.Open();
                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
Edited 15-Jan-13 2:25am
v2
Comments
Faisalabadians at 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 at 15-Jan-13 11:57am
   
It has nothing to do with Visual Studio whatsoever.
—SA
Rate this: bad
good
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.
Cheers
Andi
  Permalink  
Rate this: bad
good
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. Smile | :)
  Permalink  
Comments
Faisalabadians at 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 at 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
0 Sergey Alexandrovich Kryukov 575
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,476
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,917


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
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