Question on OleDbDataAdapters from a new guy

  • Thread starter Thread starter tom c
  • Start date Start date
T

tom c

I create 2 data OleDbDataAdapters, one with the wizard, and one in
code. I know the adapter created in code is OK because I use it to
fill a data table and I then look at one of the fields and the data is
there.

However, when I try to use the same SQL insert statement in the two
adapteres, the adapter created with the wizard works fine, but the
adapter created in code gives me an error "Object reference not set to
an instance of an object". The code is below. What am I doing wrong?

sqlA = "Select ID1, ID2 from CrossRef"
Dim daA As OleDbDataAdapter = New OleDbDataAdapter(sqlA, ConnA)
OleDbDataAdapter1.InsertCommand.CommandText = SQL 'this works fine
daA.InsertCommand.CommandText = SQL 'this gives an error

'OleDbDataAdapter1 is created by the wizard. daA is created in code.
 
Tom,
It looks like you didn't create the insert command on the one you coded.
Stop the code in the debugger and look at daA.InsertCommand to check this.
If you want to set the command then just create a SqlCommand and assign that
to the DataAdapter's InsertCommand.

Ron Allen
 
Thanks so much Ron.

You are correct. I used the debugger, set a break point, right clicked
daA, did a "Quick Watch", and I could see that insertcommand is set to
nothing.
I didn't know you could do that.

What I still don't understand is how I cerate the insert command. You
said:
If you want to set the command then just create a SqlCommand and assign that
to the DataAdapter's InsertCommand.

I thought that is what I was doing with my line of code:
daA.InsertCommand.CommandText = SQL

Apparently there is something here I don't understand. What was left
out of my original code that is needed to create the insert, update and
delete commands?

Tom
 
Tom,
You need to do something like
C#
OleDbCommand insertCmd = new OleDbCommand("..your text here...",
YourConnection);
and then
daA.InsertCommand = insertCmd;

The same applys to Delete and Update commands.

Ron Allen
 
Thanks Ron. I understand now.


Ron said:
Tom,
You need to do something like
C#
OleDbCommand insertCmd = new OleDbCommand("..your text here...",
YourConnection);
and then
daA.InsertCommand = insertCmd;

The same applys to Delete and Update commands.

Ron Allen
 
Back
Top