Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts

    adding a record in vb.net

    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()

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    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()
    I am the luckiest man in the world

  • #3
    Senior Coder
    Join Date
    Dec 2002
    Location
    Arlington, Texas USA
    Posts
    1,062
    Thanks
    4
    Thanked 8 Times in 8 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •