C
C#8mo 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
JakenVeina8mo ago
confirm the value of connectionString with the debugger
aca
aca8mo ago
I have done so
aca
aca8mo ago
No description
JakenVeina
JakenVeina8mo ago
oh the space
aca
aca8mo ago
I tried setting the connectionString explicitly now through a method call without using the configurationmanager still doesn't work Which one?
JakenVeina
JakenVeina8mo ago
"Connection Timeout"
aca
aca8mo ago
Yeah it should set the timeout value I am using it in a WPF with a messagebox
JakenVeina
JakenVeina8mo ago
right
aca
aca8mo 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
JakenVeina8mo ago
you said that remove the space
aca
aca8mo ago
Tried already, doesn't work Ill try again Yep same result
JakenVeina
JakenVeina8mo ago
try building it manually with SqlConnectionStringBuilder
aca
aca8mo ago
Check this out - apparently its readonly?
aca
aca8mo ago
No description
aca
aca8mo ago
Doesn't really makes sense in my head
JakenVeina
JakenVeina8mo ago
uh yeah
aca
aca8mo ago
No description
JakenVeina
JakenVeina8mo ago
it's not mutable on the connection
aca
aca8mo ago
Even though I am setting it in the ConnectionStringBuilder it still doesnt seem to work
JakenVeina
JakenVeina8mo ago
use SqlConnectionStringBuilder
aca
aca8mo ago
Tried so - it still buggers
JakenVeina
JakenVeina8mo ago
buggers how?
aca
aca8mo ago
The wait time is still 30 secs
aca
aca8mo ago
No description
JakenVeina
JakenVeina8mo ago
wait for what?
aca
aca8mo ago
For the connection timeout
aca
aca8mo 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
JakenVeina8mo ago
so, the connection likely isn't the problem define "wait time"
aca
aca8mo ago
For the command or connection?
JakenVeina
JakenVeina8mo ago
neither?
the wait time still goes for the default value
what is "wait time"? how are you measuring that?
aca
aca8mo 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
JakenVeina8mo ago
assumptions based on nothing
aca
aca8mo 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
JakenVeina8mo ago
what issue?
aca
aca8mo 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
JakenVeina8mo ago
the issue the user will be experiencing
which is what, specifically?
aca
aca8mo 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
JakenVeina8mo ago
great what triggers this messagebox?
aca
aca8mo ago
The try catch block
JakenVeina
JakenVeina8mo ago
what try catch block? from your initial message? I don't see a messagebox anywhere in there
aca
aca8mo 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
JakenVeina8mo ago
what exception are you getting?
aca
aca8mo 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
JakenVeina8mo 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
aca8mo ago
I fixed it by doing this
aca
aca8mo ago
No description
JakenVeina
JakenVeina8mo ago
that fixes nothing look at the exception
aca
aca8mo ago
It fixed it The messagebox is instantenous now Or wait
JakenVeina
JakenVeina8mo ago
look at the exception
aca
aca8mo ago
You're right - it fixed nothing lol How do I see it? In the debugger autos window?
JakenVeina
JakenVeina8mo 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
aca8mo ago
No description
JakenVeina
JakenVeina8mo ago
so, you said you're currently running on a VPN?
aca
aca8mo 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
JakenVeina8mo ago
see what happens if you ditch the VPN, and just specify a bogus connection
Accord
Accord8mo 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
More Posts