My basic code uses LINQ-SQL. But I have tried Kristen's method both ways- with the SqlConnectiion inside the try and before the try. In all cases I have 2 other projects where the SqlException is caught but for some reason in this particular project it throws up an SqlExceptoin window.
I have traced the code thru all 3 projects and using Kristen's method they fail on the Open(). Using my LINQ-SQL they all execute the same lines of code and fail in the at the same line in designer.cs code generated by LINQ-SQL. The
My basic code uses LINQ-SQL. But I have tried your suggested method both ways- with the SqlConnectiion inside the try and before the try. In all cases I have 2 other projects where the SqlException is caught but for some reason in this particular project it throws up an SqlExceptoin window.
I have traced the code thru all 3 projects and using your suggested method they fail on the Open(). Using my LINQ-SQL they all execute the same lines of code and fail in the at the same line in designer.cs code generated by LINQ-SQL. The
No you're right. Where the class is instantiated is irrelevant to the error. FWIW I usually write something like this:
using (var conn = new SqlConnection("connstr"))
{
try
{
conn.Open();
using (var cmd = new SqlCommand("select 1 as one", conn))
{
cmd.ExecuteReader();
// ...process result
}
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
}
}
I tried this with same results. My 2 other projects catch the error but the problem project still pops up an SqlException window on the conn.open();
Can you please verify that login Harry is defined in this database:
In SSMS, under Security/Logins, look for Harry, then right-click, script as create to clipboard, and paste the result here (masking any sensitive info)
I am purposely using a bad login to force an exception so I can test my exception logic. If I use a valid login, the connection works and the program completes normally..
OK but
I tried this with same results. My 2 other projects catch the error but the problem project still pops up an SqlException window on the conn.open();
is weird. you're in a try/catch for crying out loud. and you're catching the SqlException! It's as if the CLR thinks you're NOT in a try/catch block
I know. That's why I think it's in the environment somehow. All 3 projects are use Net framework 4.