Can't insert row


I tried to insert a row using code that worked perfectly in vs 2003.

Included is some of the code:

Dim pid As Integer = CInt(Me.ParentIDLabel1.Text)
Dim title As String = Me.TitleTextBox.Text
Dim ttext As String = Me.txtGeneralText.Text
Dim iss As Integer = Me.IssuerComboBox.SelectedValue
and so on for 12 of 13 fields, not including the autoincrementing key index

Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\HasbaraSample.mdb;Persist Security Info=True")
cn.Open()
Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT * FROM Topics", cn)
da.InsertCommand = New OleDbCommand("INSERT INTO Topics(ParentID, Title, TopicText, Issuer, BeginText, Length, TopicDate, DateType, PointOfView, ContentRights, Publisher, Active) Values(?,?,?,?,?,?,?,?,?,?,?,?)")
da.InsertCommand.Connection = cn
da.InsertCommand.Parameters.Add("@ParentID", OleDb.OleDbType.Integer, 0, "ParentID")
da.InsertCommand.Parameters.Add("@Title", OleDb.OleDbType.VarWChar, 16, "Title")
and so on for each parameter

AddHandler da.RowUpdated, AddressOf OnRowUpDated
Dim ds As New DataSet("ds1")
da.Fill(ds, "Topics")
Dim dtbl As DataTable
dtbl = ds.Tables(0)
Dim newRow As DataRow = dtbl.NewRow()
newRow("ParentID") = pid
newRow("Title") = title
newRow("TopicText") = ttext
and so on for each parameter

dtbl.Rows.Add(newRow)
da.Update(ds, "Topics")

This worked in vs studio 2003. I've seen it recommended in these forums elsewhere. However I received the following error:

system.InvalidOperationException
execute scalar: connection property has not been intialized
line238

which is da.Update(ds, "Topics")
So then I tried: da.UpdateCommand.Connection = cn
which produced the following error:

system.NullReferenceException
Object reference not set to an instance of an object.
line 238
which is: da.UpdateCommand.Connection = cn

Is there any help out there?

dennist

[2103 byte] By [dennist] at [2008-2-20]
# 1
wonder why I can't get an answer to either this or my other query? I'm sure people in this forum can either update the old fashioned way and get the autoincrementing key index. And I'm sure they can use an insertquery from the xsd designer and find a way to get the index.

I'd appreciate if someone would share the knowledge with me.

dennist

dennist at 2007-9-8 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Hi,
Is the connection object open before you call the adapter.Update Method? It should be open when your sending updates on your table...

cheers,
Paul June A. Domag

PaulDomag at 2007-9-8 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
As you can see in my code snippets I open the connection. I reviewed the entire code and nowhere do I close it.

dennist

dennist at 2007-9-8 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4
I see that you don't have an UpDateCommand. So, you shouldn't be setting the connection property on it. That explains the NullRef.
Before that you were seeing Update throw an exception "execute scalar: connection property has not been intialized". This exception I don't understand for 2 reasons, which may both be related. First of all I do see from the code that you are setting the connection property on the adapter. And secondly, the adapter.Update doesn't use ExecuteScalar, so why would it complain about that. It only uses ExecuteReader or ExecuteNonQuery.
One guess I could make is that perhaps there is an Event handler hooked to the Update (RowUpDatingEvent) and that misht be doing something erronous to cause this exception.
SirajLala at 2007-9-8 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5
thanks SirajLala, but I solved this problem long ago. I don't remember whether I solved it myself or whether somebody gave me the information.

dennist685

dennist685 at 2007-9-8 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified