Asp.net ajax refresh panel using database connection

I have a dropdown and a literal tag inside an update panel. In the dropdown selection change event, query the database and repopulate the literal tag, then call UPdatePanel.Update ().

below is there any way I can avoid having to create a new Oledbconnection every time as it seems to be slow. Can I reuse and store:

  • Data source
  • Connection on the page.

if so, how can I keep this state between calls from the GUI to the server? Here is my 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);
}

      

0


source to share


2 answers


With .NET, it is not recommended to stay connected longer than necessary. It would be good practice to put a use statement around it (so it always clears up):

string dataSource = ConfigurationSettings.AppSettings["contactsDB"];
using(var objConn = new OleDbConnection(dataSource))
{
    string id = People[cboPeople.Text];
    UpdateLiteral(objConn, id);
}

      



.NET uses pooling , which means that when you close / delete a connection, it doesn't actually close the connection, but drops it and adds it back to the pool. The next time a connection is needed, it is used from the pool. So the overhead isn't as bad as you might think, and it's not slow. In fact, you will find that it will use the same connection if only one at a time is required.

The danger with opening connections is that they never close and in high demand situations you run out of connections.

+1


source


You need to recreate this for every request. You have fewer servers. you never know when or your client will call back. You don't want to maintain an open database connection, and you cannot simply serve multiple clients by maintaining a single database connection.



To deploy high performance applications, you must use the Pools connection. When you are using .NET. The Framework Data Provider for OLE DB, you do not need to enable the join join as the provider handles this automatically. For more information on how to use pooling with .NET. Framework Data Provider for OLE DB, see OLE DB, ODBC, and Oracle Connection Pool (ADO.NET). From OleDbConnection Class

0


source







All Articles