user6631162
user6631162

Reputation:

SqlCommand cannot be sent in C#

When I insert a new personnel, that function gives me an exception.

What should I do?

public void InsertPersonnel(string conStr, string comQuery;)
{
    SqlConnection con = new SqlConnection(conStr);
    SqlCommand com = new SqlCommand(comQuery);

    con.open();

    com.ExecuteNonQuery();   // This line throws an exception
}

Upvotes: 0

Views: 81

Answers (1)

Fatih
Fatih

Reputation: 810

You did not specify the command's connection property. There are two ways to specify the connection.

1st option

SqlConnection con = new SqlConnection(conStr);
SqlCommand com = new SqlCommand(comQuery, con);

2nd option

SqlConnection con = new SqlConnection(conStr);
SqlCommand com = new SqlCommand(comQuery);

com.Connection = con;

Upvotes: 4

Related Questions