0

The error with ExecuteReader: Connection property has not been initialized is giving me a fit. I think I have it set up right. I will show you how I create the mssql connection and then what I am requesting, and the class I created to read/write and open/close the connection. (Three different parts of the app but I just lumped them together so you could see the actual logic flow I hope.)

What am I missing, or where am I supposed to put the connection? Example? I appreciate the help!

Here is the using statement I start with:

using (MSSQL mssqldb = new MSSQL(Constants.msSqlServer, Constants.msSqlDb, Constants.msSqlUid, Constants.msSqlPswd)) 

Here is where I say "Hey, get me my data using my MSSQL class"

using (var results = mssqldb.Read("SELECT TOP 1 * FROM AlertLog ORDER BY AlarmID DESC"))
{
    results.Read();
    legacyAlert = Int32.Parse(results["AlarmId"].ToString().Trim());
}

Here is the MSSQL class

class MSSQL : IDisposable
{
    public MSSQL(string server, string database, string uid, string pswd)
    {
        string msSqlConnectionString = @"Data Source=" + server + ";Initial Catalog=" + database + ";user id=" + uid + ";password=" + pswd;

        SqlConnection msqlConnection = new SqlConnection(msSqlConnectionString);
        msqlConnection.Open();
        Console.WriteLine("MS SQL OPEN!");
    }

    public void Write(string sql)
    {
        using (SqlCommand myCommand = new SqlCommand(sql, msqlConnection))
        myCommand.ExecuteNonQuery();
    }

    public SqlDataReader Read(string sql)
    {
        using (SqlCommand myCommand = new SqlCommand(sql, msqlConnection))
        return myCommand.ExecuteReader();
    }

    public void Dispose()
    {
        try {
            msqlConnection.Close();
        }
        catch (SqlException ex) {
            Console.Error.WriteLine("MS SQL Error - Closing Database");
            Console.Error.WriteLine(ex);
        }
        msqlConnection.Dispose();
    }

    private SqlConnection msqlConnection;
}
4
  • A bit off-base, but you shouldn't create a single connection to the server like that. When you're done with the connection, it should be disposed of. Commented Jan 31, 2014 at 19:11
  • I had thought about that, but I use it everywhere in the app in multiple threads, so I decided to use it from the top. I understand what you are saying, but I thought it would save me multiple connection issues. Commented Jan 31, 2014 at 19:47
  • This sounds like all those "I thought string concatenation would save me multiple parameterization issues..." arguments that happen before a web site gets hacked with SQL injection... Commented Jan 31, 2014 at 19:54
  • This is getting off topic, there is a suggestion to an answer below, but I don't understand it. If anyone could possibly show me an example of what I am doing wrong, I would appreciate it. Commented Jan 31, 2014 at 19:58

1 Answer 1

1

msqlConnection is null.

Your constructor creates a local variable named msqlConnection, but does not assign to the field.

Sign up to request clarification or add additional context in comments.

2 Comments

Still learning programming, so please excuse my ignorance. I used exactly the same way for a mysql connection and that works, but i don't understand the assign to the field part that you mean.
OK, a big DUH... thanks I figured it out after it was staring me in the face for a few minutes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.