EXECUTENONQUERY: CONNECTION PROPERTY HAS NOT BEEN INITIALIZED.

Tarun Kumar

Total Post:214

Points:1642
Posted by  Tarun Kumar
C#  .NET  SQL Server  SQL 
C#
 636  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. Uttam Misra

    Post:126

    Points:906
    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!