?
Solved

Check for SQL Connections

Posted on 2011-10-12
2
Medium Priority
?
157 Views
Last Modified: 2012-05-12
We have a major problem with connections to our databases.  There is very little that can be done until a major rewrite can happen.  (ASPX )

What I'd like to do is to check the connection upon a submit and stop all pressing from continuing if the return if false and give the user a Can Not connect to database.  Additional I don't want the error going to the logs as a sqlexception error.  

To much overhead?  Suggestions on code?
0
Comment
Question by:TimSweet220
2 Comments
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 36957160
use try...catch
try
{
   //
}
Catch(SqlException ex)
{
//show ur custom msg & write it to log
}
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36957171
Personally, I think the best bet is to look through the code and find the biggest culprits. It doesn't take much to fix bad code that create connection problems.

Example 1, connection no being closed:

SqlConnection connection = new SqlConnection(myConnString);
connection.Open();
//do something

Open in new window


fix:
SqlConnection connection = new SqlConnection(myConnString);
connection.Open();
//do something
connection.Close();

//OR

SqlConnection connection = new SqlConnection(myConnString);

try
{
connection.Open();
//do something
}
catch
{
}
finally
{
   if(connection.ConnectionState == Open)
        connection.Close();
}

//OR

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection.Open();
    //do something
}

Open in new window


Example 2, looping connections:

foreach(str in MyStrList)
{
SqlConnection connection = new SqlConnection(myConnString);
connection.Open();
//do something
connection.Close();
}

Open in new window


fix
SqlConnection connection = new SqlConnection(myConnString);
connection.Open();
foreach(str in MyStrList)
{
//do something
}
connection.Close();

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question