4

I have a sample code

aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.AddWithValue("@book_id", bookID);
aCommand.Parameters.AddWithValue("@user_id", userID);

and after that I want to execute a simple query using CommandText:

aCommand.CommandText = "SELECT * FROM aTABLE";
aCommand.ExecuteNonQuery();

but the error occurs:

Exception: Could not find stored procedure 'SELECT * FROM aTABLE'

In this case, I have to create a new instance of SqlCommand object ?

It is a way to use same SqlCommand object to avoid create one ?

3
  • 2
    You have to change the commandtype back to text before you try yo execute the query. Commented Aug 29, 2012 at 8:36
  • 3
    "In this case, I have to create a new instance of SqlCommand object ?" I would suggest to do so to avoid such confusions. Creating a SqlCommand is not so expensive that you need to reuse it. Actually the constructor does not more than setting the properties. Commented Aug 29, 2012 at 8:39
  • 1
    Not sure, but I think you should also call aCommand.Parameters.Clear(); Commented Aug 29, 2012 at 8:40

4 Answers 4

10

It should be

aCommand.CommandType = CommandType.Text

actually, the default value of CommandType is CommandType.Text

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

3 Comments

Upvoted. Good point, I have to read books :). Simple and clear.
Thanks. is this the query you want to execute? SELECT * FROM aTABLE? If so, remove the parameters you have added. @SnakeEyes
I provided an example, in that query I didn't use parameters so if SqlCommand has paramters, no matter because I didn't use them in query.
7

The problem is that you have reused a SqlCommand that CommandType is StoredProcedure but you want to execute a normal sql query with CommandType.Text.

"In this case, I have to create a new instance of SqlCommand object ?"

I would suggest to do so to avoid such confusions. Creating a SqlCommand is not so expensive that you need to reuse it. Actually the constructor does not more than setting the properties.

From ILSpy:

// System.Data.SqlClient.SqlCommand
// this() does only call _SuppressFinalize
public SqlCommand(string cmdText, SqlConnection connection) : this()
{
    this.CommandText = cmdText;
    this.Connection = connection;
}

Comments

5
aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.AddWithValue("@book_id", bookID);
aCommand.Parameters.AddWithValue("@user_id", userID);
  1. specify the stored procedure name you call

    aCommand.CommandText=yourstoredprocedurename;
    aCommand.ExecuteNonQuery();
    
  2. then call your select and sqlreader to get result

    bCommand.CommandType = CommandType.Text
    bCommand.CommandText = "SELECT * FROM aTABLE";
    SqlDataReader rdr = bCommand.ExecuteReader();
    

Comments

3

You have to construct new Command object to erase previously set parameters and value.

aCommand=new SqlCommand();
aCommand.Connection=cn;
aCommand.CommandText = "SELECT * FROM aTABLE";
SqlDataReader reader=aCommand.ExecuteReader();

Call ExecuteReader() method instead of ExecuteNonQuery to fetch the database result.

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.