none
I get an invalid operation exception when I execute the NonQuery RRS feed

  • Question

  • I am using this code:

    string connectionString = ...; //Some connection string
    SqlConnection sqlConnection = new SqlConnection(connectionString);
    sqlConnection.Open();
    
    SqlTransaction sqlTransaction = sqlConnection.BeginTransaction();
    
    SqlCommand sqlCommand = new SqlCommand();
    sqlCommand.Transaction = sqlTransaction;
    
    try
    {
    sqlCommand.CommandText = "Insert into Employee (EmpCode, EmpName) VALUES (1, 'Joydip')";
    sqlCommand.ExecuteNonQuery();
    sqlCommand.CommandText = "Insert into Dept (DeptCode, DeptName, EmpCode) VALUES (9, 'Software', 1)";
    sqlCommand.ExecuteNonQuery();
    sqlTransaction.Commit();
    //Usual code
    }

    In the first ExecuteNonQuery() I get an invalid operation exception. I don't have more information.

    Which can be the reason and how I can solve it?

    Thank so much.

    
    catch(Exception e)
    {
    sqlTransaction.Rollback();
    //Usual code
    }
    
    finally
    {
    sqlConnection.Close();
    }

    Tuesday, March 24, 2015 7:10 PM

Answers

  • Hello ComptonAlvaro,

    >>In the first ExecuteNonQuery() I get an invalid operation exception. I don't have more information.

    Does the exception contain this message “ExecuteNonQuery: Connection property has not been initialized.”? Because from your code, you do not initialize the connection property of the SqlCommand, please add this line to check if it works:

    sqlCommand.Connection = sqlConnection;

    If this does not work, please share table definition with us and the inner exception message if there is.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ComptonAlvaro Wednesday, March 25, 2015 8:13 AM
    Wednesday, March 25, 2015 8:08 AM
    Moderator

All replies

  • Hello ComptonAlvaro,

    >>In the first ExecuteNonQuery() I get an invalid operation exception. I don't have more information.

    Does the exception contain this message “ExecuteNonQuery: Connection property has not been initialized.”? Because from your code, you do not initialize the connection property of the SqlCommand, please add this line to check if it works:

    sqlCommand.Connection = sqlConnection;

    If this does not work, please share table definition with us and the inner exception message if there is.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ComptonAlvaro Wednesday, March 25, 2015 8:13 AM
    Wednesday, March 25, 2015 8:08 AM
    Moderator
  • Thank you so much, How you say, I need to set the connection to the command. I get this code from internet and I guess that the connection is was implicit inside the transaction that is set to the command, but I see that it's needed to set the connection too.

    Thank you so much.

    Wednesday, March 25, 2015 8:13 AM