System.InvalidOperationException: Internal connection fatal error - asp.net

Our staging web application on .Net 4.0 and SQL Server 2008 R2 was running smooth for more then 2 months.
All of a sudden, we started getting error in the application saying:
Error Message: System.InvalidOperationException: Internal connection fatal error.
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlDataReader.HasMoreResults()
This comes whenever my data base query is returning more then ~700 rows.
Strangely, my production web site (with the same code base as the staging) runs well while connected to the staging data base.
I have tried connecting my staging web server to the production data base (similar copy of the staging data base) and i get the same error.
This error is interminent, some times the page loads, and sometimes I get the error.
If I reduce the number of rows to less then 700, the pages work fine.
The particular Stored Proc is getting called only once in my page.
I did the following:
Modified the code to use the command object in using block.
Closing the connection immediately after the usage is done.
Compared the staging and production IIS serve settings, and they looked similar.
Same MDAC is installed on both the web servers.
But nothing helps and I still get the error in the staging web server.
The query runs fine in SQL Server Management Studio and takes < 3 sec.
Can anyone please suggest any solution to this?
This is the code base where the Stored Proc is getting called:
string strSPName = SQLSchema.strSQLSchema + SPNames.<Some SP Name>;
using (conn)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = strSPName;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = conn;
cmd.Parameters.Add(SPParams.<Some Param>, SqlDbType.Int);
cmd.Parameters[0].Value = <value of Param>;
conn.Open();
dsSubmitter = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dsSubmitter);
if (dsSubmitter.Tables.Count != 0)
{
dsSubmitter.Tables[0].TableName = DALConstants.<Table Name>;
}
conn.Close();
}
The SP is quite simple with a select statement and with a INNER join between the master and detail table. The master table has ~400 rows and detail table > 10,000 rows. The SQL Query returns around 4000 rows.

Looks like issue with SqlAdapter, why don't you try installing .net framework again.
clean up solution and rebuild it.

Related

oracle ExecuteNonQuery freezes on ASP.Net

I am trying to run a non query using a Oracle connection in ASP C# with CLR 4.5. Here is my code:
string connectionString = ConfigurationManager.ConnectionStrings["OracleConnectionString1"].ConnectionString;
OracleConnection conn = new OracleConnection(connectionString);
conn.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = conn;
cmd.CommandText = "update SALES_ADVENTUREWORKS2012.SALESORDERDETAIL set UNITPRICEDISCOUNT=0 where ROWGUID='4A399178-C0A0-447E-9973-6AB903B4AECD'";
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = QUERY_TIMEOUT;
int row_affected = cmd.ExecuteNonQuery();
HttpContext.Current.Response.Write("Rows affected:" + row_affected + "<br/>");
conn.Close();
when I run the query in oracle development tool, it works fine.
when I use the asp code above, it freezes when performing the query. It freezes forever even though I used a 5 second timeout.
I've tried using the managed and unmanaged oracle libraries; both behave the same.
Note that using the fill or scalar query work perfectly fine so there is nothing wrong with my connection string. Also the fact that oracle development can perform this update query proves that this is not a permission problem.
Any ideas?
Most likely your query is waiting to get access to the record. You probably have modified that row in "oracle development tool" and have not committed or rolled back that transaction.
Just commit/rollback in your tool or close open session.
You can check for open transactions in v$transaction view.
More on automatic locks in Oracle:
http://docs.oracle.com/cd/E11882_01/server.112/e41084/ap_locks001.htm
Are you certain you are using the 4.5 library? The 3.5 documentation states that the CommandTimeout property has no effect.
The 4.5 documentation suggests it should work, but the Remarks section doesn't mention the change, which warrants suspicion.
Otherwise, the code you posted doesn't seem to show where you actually set the value of QUERY_TIMEOUT to 5 seconds. If QUERY_TIMEOUT has a value of zero, then any other provider (SQLCommand, for example) would wait indefinitely. As vav suggested, locks from other sources could cause an indefinite wait.

asp.net MySql MySqlConnection - implement a connection pool or equivalent

It's my first time of using MySql on ASP.Net.
Unlike MSSql which I use quite often, I've noticed that using MySqlConnection to connect to the db takes ages (I mean a second or two),
MySql.Data.MySqlClient.MySqlConnection connection = new MySqlConnection(DBConnectionString);
Therefore I would like to know how can I implement a connection pool, or whatever recommended structure that could store one connection object (MySqlConnection) to be used across the application.
Is there a common practice for doing so or any other recommendations ?
Here's the code I'm using - maybe I'm doing something wrong here ?
MySql.Data.MySqlClient.MySqlConnection connection = new MySqlConnection(DBConnectionString);
MySqlDataAdapter adapter = new MySqlDataAdapter();
if (connection.State != ConnectionState.Open)
{
try
{
connection.Open();
}
catch (MySqlException ex)
{
throw (ex);
}
}
MySqlCommand cmd = new MySqlCommand("SELECT this FROM that", connection);
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
cmd.Connection.Close();
According to the documentation it's on by default. Further, you're creating the instance with the connection string, so that's good, because it allows the connector to leverage the pool immediately. So, the fact that it's taking a second or two to create those connections is almost certainly unrelated to connection pooling and more related to the hardware you're providing MySQL in the environment you're working in.
The term hardware is really broad here because you could be dealing with everything from network to disk and memory related issues.
Do read the documentation - it does show you how to adjust the connection pooling - so that may help you. I make that statement because your question doesn't give us a lot of information surrounding exactly how you're using this server or these connections.

ODBC driven ASP.NET application running on iis doesn't work on speceific system

I'm having a strange problem with IIS, asp.net and ODBC.
My application is driven by SQL server via ODBC driver (I know it's bad practice, but my entire DAL is already written and will not be changed).
The problem is that when I run an SP with my web interface, on any other computer other than the production server it works fine, but on the production server I get the following error:
Exception Message: System.Data.Odbc.OdbcException (0x80131937): ERROR [42000] [Microsoft] [ODBC SQL Server Driver][SQL Server]Error converting data type nvarchar to int
Obviously when I run it under management studio it works fine.
I think the problem is somewhere between the IIS and the odbc driver, but I'm not sure exactly where.
I'm running .net framework 4.
This is the calling method:
ODBCComm command = new ODBCComm();
command.Query = "SP_web_update_calls_dest #id=?,#name=? ,#ivrCode=?,#DDI=?,#destType=?,#trkGroup=?,#result=? output";
command.AddInputParam(id);
return AddParamsAndExecute(name, ivrCode, DDI, destType, trkGroup, command);
it basically wraps arround:
OdbcCommand.ExecuteDirect();
Thanks a lot,
Yuval.
I believe your first param is being used in the ID spot. I'm not sure why this would work in pre-production and fail only in production, but try this instead...
ODBCComm command = new ODBCComm();
command.Query = "SP_web_update_calls_dest #id=?,#name=? ,#ivrCode=?,#DDI=?,#destType=?,#trkGroup=?,#result=? output";
return AddParamsAndExecute(id, name, ivrCode, DDI, destType, trkGroup, command);
I have no idea what ODBCComm is or what AddParamsAndExecute does because you haven't included the relavant code, however, here's what the request should look like:
OdbcCommand Cmd = new OdbcCommand("SP_web_update_calls_dest", _Connection);
Cmd.CommandType = CommandType.StoredProcedure;
Cmd.Parameters.Add("#id", OdbcType.Int);
Cmd.Parameters["#id"].Value = id;
Cmd.Parameters.Add("#name", OdbcType.NVarChar);
Cmd.Parameters["#name"].Value = name;
Cmd.Parameters.Add("#ivrCode", OdbcType.Int);
Cmd.Parameters["#ivrCode"].Value = ivrCode;
Cmd.Parameters.Add("#DDI", OdbcType.VarChar);
Cmd.Parameters["#DDI"].Value = DDI;
Cmd.Parameters.Add("#destType", OdbcType.Int);
Cmd.Parameters["#destType"].Value = destType;
Cmd.Parameters.Add("#trkGroup", OdbcType.Int);
Cmd.Parameters["#trkGroup"].Value = trkGroup;
Cmd.Parameters.Add("#result", OdbcType.Int);
Cmd.Parameters["#result"].Direction = ParameterDirection.Output;
Cmd.ExecuteNonQuery();
int result = (int)Cmd.Parameters["#result"].Value;
OK,
I think I solved it.
There were actually 2 problems, and I'm not exactly sure what caused them.
Anyway, I switched the ODBC driver to SQL Server Native Client 10.
This basically solved the problem, but for some reason it doesn't support the output modifier in queries so I had to remove that.
So I got it solved, but still have no idea what caused the problem. I'm guessing it has something to do with different versions of drivers.

SQL Connection Forcibly Closed

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
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 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.
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.

Execute/Query against SQL Compact 4.0 in ASP.NET

I'm writing a small little utility MVC app and I need to have the ability to execute ad-hoc queries against my one-table SQL Compact 4.0 .sdf file for management (Web Matrix isn't working right for development, and it won't be available on the PC this will ultimately be running on). Using Entity Framework code-first, everything is working fine, but to do an ad-hoc query, I figured I'd need to connect to it the way I would have in the pre-EF days (see below)
cn = new SqlConnection("Data Source=|DataDirectory|LocalScanData.sdf");
SqlCommand cmd = new SqlCommand(query, cn);
if (cn.State != ConnectionState.Open) cn.Open();
if (query.ToUpper().StartsWith("INSERT") || query.ToUpper().StartsWith("UPDATE") || query.ToUpper().StartsWith("DELETE"))
{
TempData["RowsAffected"] = cmd.ExecuteNonQuery();
return RedirectToAction("SQL");
}
else
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return RedirectToAction("SQL", dt);
}
But when I try that, I get A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server). So, the question, is how can I connect to a SQL CE 4.0 database the old fashioned way? I've also tried using System.Data.SqlServerCe but then I get errors that lead me to believe that only works for CE 3.5 databases.
Any help?
It happened to me, too. It is the way the sdf ddbb works, you can't execute a query against that. You have to create dataset, dataadapter and so on.

Resources