0
votes

In an asp server we are getting error for SQL Server after executing ExecuteScalar, ExecuteReader and ExecuteNonQuery like shown below. The error will only come sometimes, it's not seen always. So anyone know why does this error comes? How to solve it?

System.Data.SqlClient.SqlException: 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.)

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.WriteSni()
at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode)
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
at System.Data.SqlClient.TdsParser.TdsExecuteSQLBatch(String text, Int32 timeout, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at WinMsg.Page_Load(Object sender, EventArgs e)

1
please make sure you have valid connection string. and access permission.Ravi Gadag
@Ravi He says they get that error after successful calls to the database. I can't imagine they are changing the connection strings and access permissions all willy-nilly like that.Josh Darnell
connection string is proper because the error come only few times.IT researcher
I think it may be the error in your code of calling to the database. cn you pls update ur code connect with database?felix Antony

1 Answers

0
votes

You mentioned a DataReader: please ensure that you closed it correctly. I suspect it is still open and prevent further database operations.

From MSDN:

While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader. While in this state, no other operations can be performed on the SqlConnection other than closing it. This is the case until you call the SqlDataReader.Close method. If the SqlDataReader is created with CommandBehavior set to CloseConnection, closing the SqlDataReader closes the connection automatically.

Also please ensure that SqlConnection object are closed and disposed properly after use.