How to use a connection string from web.config in a OleDbConnection control from the designer? - asp.net

My app makes use of OleDbConnection and OleDbCommand objects for executing queries. These are controls you can just drag and drop onto the designer. I can set the connection string on the OleDbConnection just fine from within the designer. The problem arises when I remove the connection string from the OleDbConnection and put it in my web/app .config. When I open/view the queries in the OleDbCommand objects, it throws this error:
"The ConnectionString property has not been initialized"
I have an OracleConnection object that doesn't throw this error but will pop up a box asking for connection string info. I don't want that either.
Before I moved the connection strings to my web.config, I was able to open the queries up in the designer (by clicking the ellipses next to the query), and even execute it if I wanted.
How can I get the designer to use the connection string inside my web.config so it stops throwing the above error when I try to open the queries from the designer?
I will note that the app works at run time because I'm setting the connection string from within the constructor of the code behind file using the ConfigurationManager.

Related

Oracle connection pooling in ASP.net

I have an old intranet website written with vb.net and using Oracle.ManagedDataAccess mostly doing read operations from Oracle database 11g.
My db connection code is as follows.
Public Shared Function MyDBConnection(ByVal command_text As String, ByVal connstring As String, ByVal ParamArray parameters As OracleParameter()) As DataTable
Dim OraCommand As New OracleCommand
Dim tmp As New DataTable
Using ORAconnection = New OracleConnection(ConfigurationManager.ConnectionStrings(connstring).ConnectionString)
OraCommand.CommandText = command_text
If parameters IsNot Nothing AndAlso parameters.Length > 0 Then
For Each p In parameters
OraCommand.Parameters.Add(p.ParameterName, p.OracleDbType, p.Size).Value = p.Value
Next p
End If
OraCommand.Connection = ORAconnection
Try
ORAconnection.Open()
tmp.Load(OraCommand.ExecuteReader())
Catch ex As OracleException
End Try
End Using
Return tmp
End Function
And my oracle connection string is like this
<add name="ConnectionString" connectionString="User Id=userid;Password=userpadd;Data Source=servername:port/port_dp"/>
I was testing if my connections to the database were closing properly but it looks like connections on the database stayed open after being closed on my code. Eventually they close way after my query completed about 2 minutes, 10 minutes or hours later.
Is this connection pooling at work? or if there is something wrong with my code?
After reading about oracle pooling, it looks like the application should be reusing the same opened connection on the DB but in my case it looks like is opening new connections anyway.
So my question is, should I disable pooling on my connection string to make sure all connections open/close and not have connections lingering on the DB?
No, you should not disable connection pooling. In .NET connection pooling is managed by a mechanism outside of your reach, and you should proceed as if it is not there: open and close your connections as you normally would (i.e. at the beginning and end of every set of operations that you wish to enroll in a transaction/every set of ops that defines a good "unit of work" such as running a report, updating a table etc
The action of opening and closing connections in your code simply leases the from/returns them to the pool. .NET will manage the rest regarding maintaining a cache of some open connections to the db
Yes, there's pooling. The number of connection might have to do with the default value of min and max pool size. Also, OracleCommand does have a Dispose method that you are not calling.
Added: I see your empty catch statement, you don't need it when using "Using" (or ever). The Dispose will still be called if there's an exception.

Using same connection string in a DataContext constructor and SqlConnection

I am preparing to deploy a web service that uses SqlConnection primarily as its means to get to the database, and I am adding some new methods that use a DataContext instead of that, and the default constructor and DBML file would use a connection string refering to my development machine (I believe...)
The connection string that the SqlConnection objects use is hard coded into a separate class (don't ask). If I provide the DataContext with the same connection string as the SqlConnections will that work instead of modifying the DBML or Web.config?
The connection string that is used in these objects is:
Dim cn As New SqlClient.SqlConnection
cd.ConnectString = ApplicationInfo.ConnectString 'Connection string is stored here
So will this work too? Do I have to change something else?
Dim db As New FADataContext(ApplicationInfo.ConnectString)
Where ApplicationInfo.ConnectString is a hard coded string that has the properties of the connection. I don't have physical access to the production SQL server or Web server therefore can't use the DBML designer to edit the connection string. And they aren't stored in the .config files.
I'm pretty sure that would work. I know when you use Entity Framework you can send a connection string in the DataContext and it works fine. Although the connection string has more information in it then regualar connection strings, like the name of the csdl, msl, ssdl or whatever.

Closing database connection in asp.net

Can we close all known/unknown connections to database with the code?
I'm using Access database and my application gives the following error:
"Could not use ''; file already in use. "
I don't know which connection is opened and no closed, so is there a way to close all application's opened connections?
When working with disposable objects you should use using so they will get disposed, and in this case even closed, when leaving the using block. Your code should look something like:
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
// Do work here; connection closed on following line.
}
Read about OleDbConnection.
UPDATE: I missed that you were accessing an access database, so updated the code to use OleDbConnection instead.

Can't rename SQLite database file after using it

I'm trying to rename my SQLite database file after I'm done using it, however the file still appears to be opened by SQLite even after all my connections are closed.
Here is an example of what I'm doing:
using (DbConnection conn = new SQLiteConnection("Data Source=test.db"))
{
conn.Open();
DbCommand command = conn.CreateCommand();
command.CommandText = "select id from test";
command.ExecuteScalar();
}
File.Move("test.db", "test.db.test");
The Move call throws an IOException. This is the only connection that I have to this database file. Once the application ends I can move the file manually without a problem. I've tried various things such as explicitly setting Pooling=false in the connection string, manually calling Close before the connection is disposed, explicitly starting and committing a transaction, but none of this seems to help. Is there a way to force SQLite to close/release the database file without exiting the application?
I presume you are using SQLite.Net? I have just tried your code and works without problem with SQLite.Net 1.0.65, in VS 2005 on Vista.
DBCommand is IDisposable. Maybe you need to call Dispose on it or create it in a using statement.

Asp.net ajax update panel sharing a database connection

I have a dropdown box and a literal tag inside an Update Panel. On the selection change event of the dropdown up requery the database and repopulate the literal tag and then call UPdatePanel.Update().
below, is there are way i can avoid having to create a new Oledbconnection each time as this seems slow. Can i reuse and store:
The Datasource
The connection in the page.
if so, how do i keep this state between calls from the GUI to the server? Here is my selection change code below
protected void cboPeople_SelectedIndexChanged(object sender, EventArgs e)
{
string dataSource = ConfigurationSettings.AppSettings["contactsDB"];
var objConn = new OleDbConnection(dataSource);
string id = People[cboPeople.Text];
UpdateLiteral(objConn, id);
}
With .NET is not a good idea to keep your connection alive longer than needs. Good practice would be to put a using statement around it (so it always gets cleaned up):
string dataSource = ConfigurationSettings.AppSettings["contactsDB"];
using(var objConn = new OleDbConnection(dataSource))
{
string id = People[cboPeople.Text];
UpdateLiteral(objConn, id);
}
.NET uses connection pooling, which means that when you close/dispose of the connection it doesn't actually close the connection, rather resets it and add it back to the pool. The next time a connection is needed it is used from the pool. So the overhead is not as much as you think and it is not slow. In actual fact you will find that it will use the same connection as long as only one at a time is needed.
The danger with keeping connections open is that they never get closed, and in high demand situations you run out of connections.
You need to recreate this for each request. You have a a state less server. you never know when or if your client will call back. You do not want to keep an open connection to the database nor could you simply service multiply clients while maintaining one database connection.
To deploy high-performance
applications, you must use connection
pooling. When you use the .NET
Framework Data Provider for OLE DB,
you do not have to enable connection
pooling because the provider manages
this automatically. For more
information about how to use
connection pooling with the .NET
Framework Data Provider for OLE DB,
see OLE DB, ODBC, and Oracle
Connection Pooling (ADO.NET).
From OleDbConnection Class

Resources