|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Question on OleDbDataAdapters from a new guyI 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 Show quote "tom c" <tomca***@gmail.com> wrote in message news:1157477463.037211.93760@e3g2000cwe.googlegroups.com... >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. > 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 I thought that is what I was doing with my line of code:> to the DataAdapter's InsertCommand. 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 Ron Allen wrote: Show quote > 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 > "tom c" <tomca***@gmail.com> wrote in message > news:1157477463.037211.93760@e3g2000cwe.googlegroups.com... > >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,
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 Show quote "tom c" <tomca***@gmail.com> wrote in message news:1157552795.046499.92940@i3g2000cwc.googlegroups.com... > 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 > > > > Ron Allen wrote: >> 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 >> "tom c" <tomca***@gmail.com> wrote in message >> news:1157477463.037211.93760@e3g2000cwe.googlegroups.com... >> >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. >> > > Thanks Ron. I understand now.
Ron Allen wrote: Show quote > 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 > "tom c" <tomca***@gmail.com> wrote in message > news:1157552795.046499.92940@i3g2000cwc.googlegroups.com... > > 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 > > > > > > > > Ron Allen wrote: > >> 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 > >> "tom c" <tomca***@gmail.com> wrote in message > >> news:1157477463.037211.93760@e3g2000cwe.googlegroups.com... > >> >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. > >> > > > |
|||||||||||||||||||||||