troubleshooting Question

C# executing a command after opening connection to SQL database

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
DatabasesC#SQL
17 Comments2 Solutions263 ViewsLast Modified:
I have this method that gets the connection string. I first have a method that builds the connection string. Then I open the connection and finally I try to execute a SQL stored procedure against this connection. As soon as I try to execute the sp I get the message "Connection property has not been initialized" What am I missing and how do I correct this?

        public static SqlConnection GetConnection(string ConnectionString)
        {
            /* Create and open a connection to the SQL Server */
            /* Return open connection object */
            SqlConnection SQLConn = new SqlConnection();

            string loginType = Properties.Settings.Default.SQLLogintype.ToUpper();

            try
            {
                if (loginType == "WINDOWS")
                {
                    SQLConn.ConnectionString = ConnectionStringWindows(ConnectionString);
                    SQLConn.Open();
                }
                if(loginType == "SQL")
                {
                    SQLConn.ConnectionString = ConnectionStringSQL(ConnectionString);
                    SQLConn.Open();
                }

                return SQLConn;
            }
            catch (Exception ex)
            {
                Log.Write("Failed to get SQL connection to the database " + Controller.Instance.Model.SQLDatabase + ": " + ex.Message);
                Log.Write(SQLConn.ConnectionString);
                throw ex;
            }

        }
===============================================================================================
/* Build the connection string */
        public static string ConnectionStringSQL(string ConnectionString)
        {
            /* Return connection string for ".Net Framework Data Provider for SQL Server" (System.Data.SqlClient.SqlConnection) */
            return @"Data Source=" + Properties.Settings.Default.SQLSERVER + ";Initial Catalog=" + Properties.Settings.Default.SQLDatabase + ";User Id=" + Properties.Settings.Default.SQLUserID + ";Password=" + Properties.Settings.Default.SQLPassword + ";" + Properties.Settings.Default.SQLOption;
        }
===============================================================================================
            /* Open the connection */
            SqlConnection DataConnection = DataAccess.GetConnection(ConnectionString);
===============================================================================================
Execute stored procedure to insert record
            cmd.Connection = DataConnection;
            cmd.ExecuteNonQuery();
ASKER CERTIFIED SOLUTION
Snarf0001

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros