ExecuteNonQuery: Connection property has not been initialized.

Total Post:214

Points:1642
C#  .NET  SQL Server  SQL 
C#
 1000  View(s)
Ratings:
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

  1. Post:127

    Points:915
    Re: ExecuteNonQuery: Connection property has not been initialized.

    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();

Answer