Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate ConnectionString exists and if not do not throw error

The connection name 'MySqlServer' was not found in the applications configuration or the connection string is empty.

So, I have a page with a panel that will display when the connection in the web config is found and the connection is valid; using a try/catch as long as the add name"VALUE" is in the config connection strings if the server data is bad the page will load and the panel is set to invisible... I need to be able to handle the following...

If the named value in this case MySqlServer is used in the aspx; aspx.cs but not found in the config I do not want the error to occur; connection name was not found.... I just want to not show the panel like when the SqlConnection.Open fails when the name is found but data is bad...

aspx

   <asp:SqlDataSource runat="server" ID="allowedIPsSqlDataSource" 
    ConnectionString="<%$ ConnectionStrings:MySqlServer %>"

aspx.cs

string connectionString = ConfigurationManager.ConnectionStrings["MySqlServer"].ToString();
        SqlConnection SqlConnection = new SqlConnection(connectionString);
        SqlCommand SqlCommand = new SqlCommand();
        try
        {
            SqlConnection.Open();

config

<connectionStrings>
        <add name="NotMySqlServer" providerName="System.Data.SqlClient" connectionString="server=TEST\SQL2005;database=ADB;Integrated Security=True"/>
<add name="NotMy2SqlServer" providerName="System.Data.SqlClient" connectionString="server=TEST\SQL2005;database=ADB;Integrated Security=True"/>

    </connectionStrings>
like image 586
Barry Avatar asked Jul 06 '11 16:07

Barry


3 Answers

You can try :

if(ConfigurationManager.ConnectionStrings["MySqlServer"] == null) throw ...
like image 137
Jozef Cechovsky Avatar answered Oct 16 '22 21:10

Jozef Cechovsky


If you're using .NET 4.5+ and have access to C# 6.0, you can make use of the null conditional operator (?) to try and get the connection string without automatically throwing an exception:

string connectionString = ConfigurationManager.ConnectionStrings["MySqlServer"]?.ConnectionString;
//------------------------------------------------------------------------HERE-^-HERE-------------

if (string.IsNullOrWhiteSpace(connectionString))
{
    // Don't even bother trying to open the connection.
    // Log the error and either rethrow the exception (throw;) or exit from your current context (return;).
    //return;
    //throw;
}

// If your code has made it this far, it means you have a valid connection string.  Now try to use it.
using (var sqlConnection = new SqlConnection(connectionString))
{
    sqlConnection.Open();

    using (var sqlCommand = new SqlCommand)
    {
        // Do stuff.
    }
}
like image 27
Mass Dot Net Avatar answered Oct 16 '22 20:10

Mass Dot Net


You can check if there are any connections strings by using count.

var count = ConfigurationManager.ConnectionStrings.Count;
if (count > 0)
{
    //There is at least more then one connection string.
}

Update

public static class Extension
{
    public static bool HasConnectionString(this ConnectionStringSettingsCollection value, string key)
{
    try
    {
        return value[key].ConnectionString.Length > 0;
    }catch 
    {
        return false;
    }
}
}

You can use the extension as follow.

if (ConfigurationManager.ConnectionStrings.HasConnectionString("MySqlServer"))
{
    //If true you know there is a valid connectionstring.
}
like image 34
Jethro Avatar answered Oct 16 '22 22:10

Jethro