0

I'm trying to write a general function for running SQL commands that only bring back one result.

I'm getting the following error:

Exception Details: System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.

Source Error: 

Line 130:
Line 131:            SQLCommand.CommandText = SQL;
Line 132:            String myResult = (String)SQLCommand.ExecuteScalar();
Line 133:
Line 134:            return myResult;

Source File: c:\Development\pros\Functions.cs    Line: 132 

The code is:

public static string SingleSQL(string SQL)
{

    SqlConnection SQLCON = new SqlConnection(ConfigurationManager.ConnectionStrings["PIDDBConnectionString"].ToString());

    SQLCON.Open();
    SqlCommand SQLCommand = new SqlCommand();
    SQLCommand.CommandType = CommandType.Text;

    SQLCommand.CommandText = SQL;
    String myResult = (String)SQLCommand.ExecuteScalar();

    return myResult;

}

2 Answers 2

3

You do not assign the connection to the SqlCommand object.

Try something like

public static string SingleSQL(string SQL)
{

    SqlConnection SQLCON = new SqlConnection(ConfigurationManager.ConnectionStrings["PIDDBConnectionString"].ToString());

    SqlCommand SQLCommand = new SqlCommand();
    SQLCommand.Connection = SQLCON;
    SQLCommand.CommandType = CommandType.Text;

    SQLCommand.CommandText = SQL;

    try
    {
       SQLCON.Open();
       return (String)SQLCommand.ExecuteScalar();
    } finally {
       if (SQLCON.State != ConnectionState.Closed) SQLCON.Close();
    }
}

You should be able to change the function return type to object. The calling function is then able to convert as required.

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

5 Comments

Thanks for the help, that gives: Error 5 The best overloaded method match for 'System.Data.SqlClient.SqlCommand.SqlCommand(string)' has some invalid arguments C:\Development\foos\Functions.cs 127 37 PIDs Error 6 Argument 1: cannot convert from 'System.Data.SqlClient.SqlConnection' to 'string' C:\Development\fooss\Functions.cs 127 52 PIDs Error 7 The name 'myResult' does not exist in the current context C:\Development\foos\Functions.cs 142 20 PIDs
@TomBeech I have edited the code, try again. I incorrectly used the connection string as the first parameter. Writing this from memory without test :)
You don't need to open the connection before assigning it to the command? I have this feeling that's your problem.
You also have to declare myResult outside of the try block, else it doesn't exist in the scope where you're returning it.
@DanJ Thanks for the catch, added a return statement as the variable is not really needed.
0

You'd better use

SQLCON.CreateCommand()

This will always assure you have the command associated with the connection you want.

Why is this better? Assume you go use another database platform, then you don't have to change the code for the command (since the command returned is of type IDbCommand which is the interface behind the SqlCommand, etc.)

Comments

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.