C
C#14mo ago
aca

❔ Connection Timeout argument passed doesn't work for a SqlConnection

I have the following connection string:
<add name="DatabaseServerInstance"
connectionString="Server=xx.xx.x.xx; Database=db; User Id=user; Password=pass; TrustServerCertificate=true; Connection Timeout=2;" />
<add name="DatabaseServerInstance"
connectionString="Server=xx.xx.x.xx; Database=db; User Id=user; Password=pass; TrustServerCertificate=true; Connection Timeout=2;" />
I have a WPF with a control calling a MessageBox to show whether the connection to the database is established or not and I want Connection.Open() to terminate early instead of the user having to wait 15 seconds for the failed connection message to be displayed, but Connection Timeout=2 seems to do nothing. An open connection gives an instantaneous response, while the failed connection despite the argument, loads with the average time no matter what the timeout is set to. This is the code for the sqlconnection call:
public class SqlPusher
{
private static SqlCommand command = new SqlCommand();
private static string connectionString = ConfigurationManager.ConnectionStrings["DatabaseServerInstance"].ConnectionString;

public Boolean TestConnection()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}

}
public class SqlPusher
{
private static SqlCommand command = new SqlCommand();
private static string connectionString = ConfigurationManager.ConnectionStrings["DatabaseServerInstance"].ConnectionString;

public Boolean TestConnection()
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
return false;
}
}
}

}
57 Replies
JakenVeina
JakenVeina14mo ago
confirm the value of connectionString with the debugger
aca
acaOP14mo ago
I have done so
aca
acaOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
oh the space
aca
acaOP14mo ago
I tried setting the connectionString explicitly now through a method call without using the configurationmanager still doesn't work Which one?
JakenVeina
JakenVeina14mo ago
"Connection Timeout"
aca
acaOP14mo ago
Yeah it should set the timeout value I am using it in a WPF with a messagebox
JakenVeina
JakenVeina14mo ago
right
aca
acaOP14mo ago
And I want a quick response to the message box instead of the user having to wait 15 secs for a failed connection
JakenVeina
JakenVeina14mo ago
you said that remove the space
aca
acaOP14mo ago
Tried already, doesn't work Ill try again Yep same result
JakenVeina
JakenVeina14mo ago
try building it manually with SqlConnectionStringBuilder
aca
acaOP14mo ago
Check this out - apparently its readonly?
aca
acaOP14mo ago
No description
aca
acaOP14mo ago
Doesn't really makes sense in my head
JakenVeina
JakenVeina14mo ago
uh yeah
aca
acaOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
it's not mutable on the connection
aca
acaOP14mo ago
Even though I am setting it in the ConnectionStringBuilder it still doesnt seem to work
JakenVeina
JakenVeina14mo ago
use SqlConnectionStringBuilder
aca
acaOP14mo ago
Tried so - it still buggers
JakenVeina
JakenVeina14mo ago
buggers how?
aca
acaOP14mo ago
The wait time is still 30 secs
aca
acaOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
wait for what?
aca
acaOP14mo ago
For the connection timeout
aca
acaOP14mo ago
Check it out even the debugger confirms both the timeout for the command and connection is 2 secs and yet the wait time still goes for the default value
No description
JakenVeina
JakenVeina14mo ago
so, the connection likely isn't the problem define "wait time"
aca
acaOP14mo ago
For the command or connection?
JakenVeina
JakenVeina14mo ago
neither?
the wait time still goes for the default value
what is "wait time"? how are you measuring that?
aca
acaOP14mo ago
Well either it's CommandTimeout or ConnectionTimeout But we have confirmed these by the debugger to be 2 seconds, so it doesn't make sense what could be causing the issue
JakenVeina
JakenVeina14mo ago
assumptions based on nothing
aca
acaOP14mo ago
Well I can't seem to narrow down the issue in any other way I have no idea what could be causing the problem
JakenVeina
JakenVeina14mo ago
what issue?
aca
acaOP14mo ago
The issue I am experiencing with the wait time Or rather, the issue the user will be experiencing Perhaps I could ping the database and return a boolean value? Dose it make sense to use the Open.Connection to test whether a connection can be established?
JakenVeina
JakenVeina14mo ago
the issue the user will be experiencing
which is what, specifically?
aca
acaOP14mo ago
Waiting 30 seconds on a stale screen with the WPF for the messagebox to be returned stating the user could not connect to the database
JakenVeina
JakenVeina14mo ago
great what triggers this messagebox?
aca
acaOP14mo ago
The try catch block
JakenVeina
JakenVeina14mo ago
what try catch block? from your initial message? I don't see a messagebox anywhere in there
aca
acaOP14mo ago
I'll have to screenshot the WPF XAML and my ViewModel class But basically the try catch catches any sqlexceptions which prompts the messagebox And the wait time is long due to the connection.Open() trying to load for 30 secs instead of the defined time Ill try something else
JakenVeina
JakenVeina14mo ago
what exception are you getting?
aca
acaOP14mo ago
Basing it on ConnectionState instead to see if it speeds up things as the value is based on an enum instead of trying to establish a connection Doesnt matter I am catching any sql exception - if my VPN is not toggled on it fails as it should
JakenVeina
JakenVeina14mo ago
nonsense look at the exception you're trying to determine why an exception is occurring, but the exception doesn't matter? nothing could be more meaningful
aca
acaOP14mo ago
I fixed it by doing this
aca
acaOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
that fixes nothing look at the exception
aca
acaOP14mo ago
It fixed it The messagebox is instantenous now Or wait
JakenVeina
JakenVeina14mo ago
look at the exception
aca
acaOP14mo ago
You're right - it fixed nothing lol How do I see it? In the debugger autos window?
JakenVeina
JakenVeina14mo ago
excpetions should be pausing the debugger the moment they throw ensure that all excpetions are selected for first-chance catching, withing the "Exception Settings" widget
aca
acaOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
so, you said you're currently running on a VPN?
aca
acaOP14mo ago
The WPF is developed for usage with a VPN I am testing the functionality of the feature by trying to turning the VPN off and testing the database conection The program is working as intended but I want the prompted messagebox to appear faster by decreasing the connection timeout
JakenVeina
JakenVeina14mo ago
see what happens if you ditch the VPN, and just specify a bogus connection
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server