Sql Exception was caught

I have a method like this

        public static DataSet GetAllDataBaseNames()
        {

        //Instance of connection is created
        SqlConnection sConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

        //To Open the connection.
        sConnection.Open();
        string selectDatabase = @"SELECT   
                                        [NAME]
                                    FROM     
                                        [master..sysdatabases]";

        //Instance of command is created.
        SqlCommand sCommand = new SqlCommand(selectDatabase, sConnection);

        try
            {
            //Create the dataset.
            DataSet dsListOfDatabases = new DataSet("master..sysdatabases");

            //Create the dataadapter object.
            SqlDataAdapter da = new SqlDataAdapter(selectDatabase, sConnection);

            //Provides the master mapping between the sourcr table and system.data.datatable
            da.TableMappings.Add("Table", "master..sysdatabases");

            //Fill the dataadapter.
            da.Fill(dsListOfDatabases);

            //Bind the result combobox with non primary key table names
            DataViewManager dsv = dsListOfDatabases.DefaultViewManager;
            return dsListOfDatabases;
            }
        catch(Exception ex)
            {
            //Handles the exception and log that to the EventLog with the original message.
            EventLog log = new EventLog("Application");
            log.Source = "MFDBAnalyser";
            log.WriteEntry(ex.Message);
            return null;
            }

        finally
            {
            //checks whether the connection is still open.
            if(sConnection.State != ConnectionState.Closed)
                {
                sConnection.Close();
                }
            }

        }

But when I call it like this

        public void BindDBDropDown()
        {

          DataSet dsTablesWithoutForeignKeys = default(DataSet);

        try
            {
            //The function GetAllForeignKeyTables() is called from the class PluginManager.
            dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames();

            dgResultView.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"];
            }
        catch(Exception ex)
            {
            //All the exceptions are handled and written in the EventLog.
            EventLog logException = new EventLog("Application");
            logException.Source = "MFDBAnalyser";
            logException.WriteEntry(ex.Message);
            }
        }

It is getting the sql exception caught in the catch block

Can you guys please have a look at that.

Answers


Try:

SELECT   [name]
FROM     [master]..[sysdatabases] // **not** [master..sysdatabases]

(note the square brackets)

Also; it would be far preferable to use using for SqlConnection, SqlCommand, etc - everything that implements IDisposable - this will ensure you don't haemorrhage connections.

i.e.

using(var sConnection = new
    SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
{

    //To Open the connection.
    sConnection.Open();
    string selectDatabase = @"
        SELECT    [NAME]
        FROM    [master]..[sysdatabases]";

    //Instance of command is created.
    using(var sCommand = new SqlCommand(selectDatabase, sConnection)) {
         // etc
    }
}

Need Your Help

Proper way to unit test beforeFilter() and beforeRender() in AppController

php unit-testing cakephp cakephp-2.0

I'm writing unit tests for a new site I'm working on that uses CakePHP 2.3. Everything's going smoothly, except for one thing. I want to test the beforeFilter() and beforeRender() functions.

Membership/Forum Platform: Turn SMF into Portal, Import into Joomla, or Do Something Else?

content-management-system joomla forum

I've 1) a PHP-based membership site and 2) some SMF forums. I want to integrate these things, so that they have a shared sign-on; I also want to run some PHP functions that will determine the items...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.