Inserting A New Record

  • Thread starter Thread starter Curt Emich
  • Start date Start date
C

Curt Emich

Can someone tell me specifically what's missing in this code so that it
won't write the record into the database? Please, no references to lengthy,
bloated, meandering articles telling me how to make a watch when I'm merely
asking what time it is.



Dim thisDataAdapter As OleDb.OleDbDataAdapter = New
OleDb.OleDbDataAdapter("SELECT * FROM Sessions", OleDbConnection1)

thisDataAdapter.InsertCommand = New OleDb.OleDbCommand("INSERT INTO Sessions
SET Notes = ? ", OleDbConnection1)

thisDataAdapter.InsertCommand.Parameters.Add("Notes", OleDbType.VarChar, 15,
"Notes")

' Dim workParm As OleDbParameter =
catDA.UpdateCommand.Parameters.Add("@CategoryID", OleDbType.Integer)

' workParm.SourceColumn = "CategoryID"

' workParm.SourceVersion = DataRowVersion.Original

Dim thisDataset As DataSet = New DataSet()

thisDataAdapter.Fill(thisDataset, "Sessions")

Dim thisTable As New DataTable()

thisTable = thisDataset.Tables("Sessions")

Dim cRow As DataRow = thisTable.NewRow()

cRow("Notes") = "Yadda"

thisTable.Rows.Add(cRow)

thisDataAdapter.Update(thisDataset)
 
Curt,

The logic of the table assignment is backwards. You're trying to update the
permanent table 'sessions' but you're actually updating the temporary table
'thistable'.

Do this instead:
crow = thisdataset.Tables(0).NewRow()

etc.

HTH,

Bernie Yaeger
 
Hi Curt,

Try, for starters, using a simple commandbuilder, as in this very direct and
simple code snippet:
Dim davwbillex As New SqlDataAdapter("select * from vwbillex", oconn)

Dim dsvwbillex As New DataSet("vwbillex")

davwbillex.Fill(dsvwbillex, "vwbillex")

Dim commandbuilder_vwbillex As SqlCommandBuilder = New
SqlCommandBuilder(davwbillex)

mrow = dsvwbillex.Tables(0).NewRow()

mrow("retcode") = "54901"

mrow("inv_dt") = Date.Now.Date

dsvwbillex.Tables("vwbillex").Rows.Add(mrow)

Try

davwbillex.Update(dsvwbillex, "vwbillex")

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

If this works ok (you'll need, of course, a table with this structure), then
there's nothing wrong with the connection.

Let me know.

Regards,

Bernie
 
Back
Top