Click here to Skip to main content
16,004,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I read somewhere that if we try to change the Connection property of SqlCommand Object when connection to the database already exists, it throws a Invalid Operation Exception. I wrote a small code to test this

C#
SqlCommand cmd = new SqlCommand("Select * from Authors",con); //connection is defined
con.Open();  // connection is opened
                cmd.Connection=con1; // another valid connection object


but it is not throwing any exception.
Please help me what was wrong in my understanding and in which case the Invalid Operation Exception is thrown by SqlCommand object.

Thanks in advance
Posted
Comments
ZurdoDev 7-Mar-12 11:28am    
The command has not been executed. It has simply been linked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900