Take the 2-minute tour ×

I'm trying to connect to database asynchronously in WCF service. But despite of setting "Asynchronous Processing=true" in connection string I'm getting

System.InvalidOperationException with message BeginExecuteReader: Connection property has not been initialized.

The code I'm using to connect to database is:

public void Connect()
   using (SqlConnection conn = new SqlConnection("Data Source=User12-PC; Initial Catalog = BMS;  User Id=sa; Password = pass; Asynchronous Processing=true"))
      SqlCommand command = new SqlCommand();
      command.CommandText = "Select l.currvalue from" +
                            " advt_ctrl_pts as p inner join advt_log_in_ctrl_pts l" +
                            " on p.registerid = l.regid and p.ddcid = l.ddcid" +
                            " where p.pointid = 5156102" +
                            " order by datetime";

      command.CommandType = CommandType.Text;

      IAsyncResult result = command.BeginExecuteReader(); //This part is returning exception
      if (result.IsCompleted)
         timer = new Timer(new TimerCallback(onTimerTick), command.EndExecuteReader(result), 5000, 5000);

Could anyone please tell me what's wrong I'm doing?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

you did not assign connection to command:

SqlCommand command = new SqlCommand();
command.Connection = conn;
share|improve this answer
Can also use new SqlCommand("select ...", conn); etc.. –  Tony D Feb 22 '13 at 8:42

Your Answer


