transaction problem

  • Thread starter Thread starter denis
  • Start date Start date
D

denis

Hello I'm trying to run this code but i get an error:
"Object reference not set to an instance of an object. "

Can somebody help me?
Thanks


Try

'***************************************************************************
********
objTransact = objConnection.BeginTransaction()

'***************************************************************************
********

strSQL = "INSERT INTO Artisti (a_nome) values ('pippo')"

myDataAdapter = New OleDbDataAdapter( strSQL, objConnection )
myDataAdapter.InsertCommand.Transaction = objTransact
myDataSet = New DataSet()
myDataAdapter.Fill( myDataSet, "Artisti")
myDataTable = myDataSet.Tables("Artisti")

'Crea u datarow con il record da inserire
myDataRow = myDataTable.NewRow()
myDataRow("a_nome") = Request.Form("NOME")
myDataRow("a_cognome") = Request.Form("COGNOME")
myDataRow("a_usr") = Request.Form("USR")
myDataRow("a_pwd") = Request.Form("PWD")
myDataRow("a_azienda") = Request.Form("AZIENDA")
myDataRow("a_indirizzo") = Request.Form("INDIRIZZO")
myDataRow("a_cap") = Request.Form("CAP")
myDataTable.Rows.Add(myDataRow)

DBCommandBuilder = New OleDbCommandBuilder(myDataAdapter)
myDataAdapter.InsertCommand = DBCommandBuilder.GetInsertCommand()
myDataAdapter.Update(myDataSet, "Artisti")
objTransact.Commit()

catch ex as exception
lblStatus.Text += "Error:<br>" & ex.Message
objTransact.Rollback()
Finally
objConnection.Close()
End try
 
denis said:
Hello I'm trying to run this code but i get an error:
"Object reference not set to an instance of an object. "

It would help if you could tell us which line the error is occurring
on...
 
denis said:
error occures at this line:
myDataAdapter.InsertCommand.Transaction = objTransact

Right. That would presumably be because you haven't set up the insert
command at that point.
 
denis said:
what exactly do I have to do?

Exactly what you're doing later - just do it earlier on.

However, if this kind of problem is troubling you, I seriously suggest
that you master the basics of .NET before going any further. I find
it's usually a lot quicker to learn the basics of a platform thoroughly
before progressing to relatively complicated stuff (like database
access) than to dive in at the deep end, trying to do everything at
once.
 
It's hours I'm searching the web for the solution
I've found other people with the same error but without any response

I can't understand what you mean (doing later - just do it earlier on)
Can you correct the error in my code? and give me the right snippet?

I think this post could be very helpfull for other also

P.S. I'm not trying to do everything at once, first I've created the script
for updating a row
now I'm tryng to add a transaction, then I will go on

Thanks for your help
 
denis said:
It's hours I'm searching the web for the solution
I've found other people with the same error but without any response

I can't understand what you mean (doing later - just do it earlier on)
Can you correct the error in my code? and give me the right snippet?

Just move the lines which set up the InsertCommand to before you try to
*use* the InsertCommand - or vice versa.
I think this post could be very helpfull for other also

P.S. I'm not trying to do everything at once, first I've created the
script for updating a row now I'm tryng to add a transaction, then I
will go on

You're still trying to learn ADO.NET without understanding the basics
of .NET first though - that's the real problem here.
 
Back
Top