Question

I'm having difficulty with an SQL query against Server 2008 from IIS7. I have a VB.NET class library which runs an update statement. The underlying code used to create the connection hasn't changed, but suddenly the query is failing in our testing and development environments. It does, however, still work against the same server/database using the slightly older code in our production environment.

I've tried setting the connection timeout in the web.config and I'm at a loss to explain the cause.

The basic structure of the query is:

Dim conn = New SqlConnection()
conn.ConnectionString = "Data Source=someserver\sqlexpress2008;Initial Catalog=DatabaseName;User ID=sa;Password=pass"
conn.Open()
Using cmd As SqlCommand = conn.CreateCommand()
    cmd.CommandText = "UPDATE ..."
    cmd.Parameters.AddWithValue("@UName", user.name)
    cmd.ExecuteNonQuery() 'fails with error
End Using

The error is:

A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

I've tried restarting IIS and the SQL server and I'm totally out of ideas. I just need a fix

Was it helpful?

Solution 3

This was a nightmare to track down. It turned out to be cause by a horrible quirk in VB.NET. Nullable datetimes seem to be coerced to DateTime.MinValue, which resulted in a DateTime.MinValue being inserted into an sql datetime. The fix was to check for either !property.HasValue && property.Value != DateTime.MinValue when setting the parameters for the command.

OTHER TIPS

You need to open the connection before calling SqlCommand.ExecuteNonQuery(). You do this by calling SqlConnection.Open().

Dim conn = New SqlConnection() 
conn.ConnectionString = "Data Source=someserver\sqlexpress2008;Initial Catalog=DatabaseName;User ID=sa;Password=pass" 
Using cmd As SqlCommand = conn.CreateCommand() 
    cmd.CommandText = "UPDATE ..." 
    cmd.Parameters.AddWithValue("@UName", user.name) 

    conn.Open()
    cmd.ExecuteNonQuery() 'fails with error 
    conn.Close()
End Using 

Also, ensure you database isn't in single user mode.

This helped another person who was stuck recently. You could examine the problem from the database server by setting up a SQL Server Profiler.

You can find lots of info about SQL Profiler by just googling around. Here's a site with a video that might help you get started. For starters, you would be able to see if the request is even reaching the database server.

This is a network-level error. The database server is killing the connection for some reason. In order to troubleshoot this, I would open a connection using SSMS to the DEV and TEST servers and make sure that I can run simple queries w/o problems. It's unlikely that the issue is your library since you would be getting timeout or some other kind of errors.

as Lcarus, said, database server is killing the connection for unknown reason. you can check the logs, to verfiy. Log path will be C:\Program Files\Microsoft SQL Server\<your instance>\MSSQL\LOG

from MSDN Blog MSDN Blog

this will occur when A connection is taken from the connection pool, the application does not know that the physical connection is gone, an attempt to use it is done under the assumption that the physical connection is still there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top