PDA

View Full Version : adding a record in vb.net



miranda
01-14-2003, 05:17 AM
I have an access database with a table(serials) with 3 fields
SerialNum, Model, SixDigits

I want to add a new record. here is the code I have

DsSerial1 is the name of the dataset

the three variables i have that hold the values to be passed are
Dim newSerialNum, model As String
Dim newSerial As Integer



Dim drNew As System.Data.DataRow
drNew = Me.DsSerial1.serials.NewRow
drNew.Item("SerialNum") = newSerialNumber
drNew.Item("Model") = model
drNew.Item("SixDigits") = newSerial
Me.DsSerial1.serials.Rows.Add(drNew)


Any ideas on why this doesnt work but if i use the following ado this works

Dim rsADO As New ADODB.Recordset()
Dim cnADO As ADODB.Connection
cnADO = create_connection()
cnADO.Open()
rsADO.ActiveConnection = cnADO
rsADO.Open("SELECT * FROM serials", cnADO, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
rsADO.AddNew()
rsADO.Fields("SerialNum").Value = newSerialNumber
rsADO.Fields("Model").Value = model
rsADO.Fields("SixDigits").Value = newSerial
rsADO.Update()

rsADO.Close()
cnADO.Close()

Roelf
01-14-2003, 07:24 AM
i'm not sure, don't know vb.net good enough, but looking at your code i should think after the Me.DsSerial1.serials.Rows.Add(drNew) command, ther should probably be a sort of update statement. so the database is updated with the new record, like you do in regular VB with rsADO.Update()

miranda
01-15-2003, 12:52 AM
Roelf

You were close.

here is what i ended up doing

Dim drNew As System.Data.DataRow
drNew = Me.DsSerial1.serials.NewRow
drNew.Item("SerialNum") = newSerialNumber
drNew.Item("Model") = model
drNew.Item("SixDigits") = newSerial
Try
Me.DsSerial1.serials.Rows.Add(drNew)
Catch ex As Exception
MessageBox.Show("Add Serial Number" & vbCr & ex.Message)
Exit Sub
Finally
Cursor.Current = Cursors.Default
End Try
If DsSerial1.HasChanges() Then
Try
DaSerials.Update(DsSerial1.serials)
Catch ex As Exception
MessageBox.Show("Update Serial Number Table" & vbCr & ex.Message)
End Try
End If