The DataAdapter.SelectCommand property needs to be initialized.

acnice

Member
Joined
Feb 18, 2010
Messages
10
Location
Colombo , Sri Lanka
Programming Experience
Beginner
please help me..when running this code it gives an exception called:The DataAdapter.SelectCommand property needs to be initialized.
this is my code:

Dim drEmployee As DataRow
drEmployee = dsEmployee.Tables("Employee").NewRow()

With drEmployee
.Item("empNo") = txtEmpNo.Text
.Item("empName") = txtEmpName.Text
.Item("empDOB") = dtpDOB.Value
.Item("Gender") = chrGender
.Item("deptNo") = cboDepartment.SelectedIndex + 1
.Item("empSalary") = txtSalary.Text
End With

dsEmployee.Tables("Employee").Rows.Add(drEmployee)

adEmployee.InsertCommand = cmdBuilder.GetInsertCommand
 
First things first, get rid of the line that's throwing the exception. There is absolutely no point calling GetInsertCommand and assigning the result manually to the DataAdapter's InsertCommand if you aren't going to make any changes to it. If you're not going to make any changes to the command, e.g. enlist it in a transaction, then you should simply call Update on the DataAdapter and let the rest happen automatically.

That said, the CommandBuilder is not magic. It can't generate an INSERT statement with no knowledge of the table and columns being inserted into. In order to use a CommandBuilder with a DataAdapter, the DataAdapter's SelectCommand must contain a SQL SELECT statement. Only by your specifying where the data has originally come from can the CommandBuilder know where the new data is going to.
 
Back
Top