Home > DeveloperSection > Forums > ExecuteNonQuery: Connection property has not been initialized.
Tarun Kumar

Total Post:210

Points:1614
Posted on    February-23-2016 3:59 AM

 C# C#  .NET  SQL Server  SQL 
Ratings:


 1 Reply(s)
 257  View(s)
Rate this:
I am getting error "ExecuteNonQuery: Connection property has not been initialized." while executing command in C#.

SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "My Insert Query";
                cmd.ExecuteNonQuery();

Please help



Uttam Misra

Total Post:124

Points:888
Supported
Posted on    February-23-2016 4:03 AM

You forgot to assign connection to the SqlCommand, you can use the constructor or property
use following line of code

SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandText = "My Insert Query";
                cmd.ExecuteNonQuery();


Don't want to miss updates? Please click the below button!

Follow MindStick