Results 1 to 4 of 4

Thread: Add record into dataset

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2002
    Posts
    22

    Add record into dataset

    How to I add, Update, Delete and search from the dataset

    I want to create a program allow user add, update, delete or search the record from the dataset. after the user click save button then those record only save into database.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    First read:
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemDataDataTableClassTopic.htm

    To add a row, you use the method DataSet.Table.NewRow to initialize a new row and associate it with a DataRow object, then set the values of the DataRow to what you want, then invoke DataSet.Table.Rows.Add to add it to the dataset, like so (cut and pasted from a game utility I work on):
    Code:
            Dim objRow As DataRow
            objRow = dsGurpsData.Tables("Attribute").NewRow
            objRow("FullName") = "Strength"
            objRow("ShortName") = "ST"
            objRow("Value") = 10
            objRow("ModifiedValue") = 10
            dsGurpsData.Tables("Attribute").Rows.Add(objRow)
    To remove a record, you first find the record and associate it with a DataRow object using either DataSet.Table.Rows.Find, or DataSet.Table.Select; then you invoke DataSet.Table.Rows.Remove like so:
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdatadatarowcollectionclassremovetopic.htm
    Code:
    Private Sub RemoveFoundRow(ByVal myTable As DataTable)
        Dim myDataRowCollection As DataRowCollection
        Dim foundRow As DataRow
        myDataRowCollection = myTable.Rows
        ' Test to see if the collection contains the value.
        If myDataRowCollection.Contains(TextBox1.Text) Then
           foundRow = myDataRowCollection.Find(TextBox1.Text)
           myDataRowCollection.Remove(foundRow)
           Console.WriteLine("Row Deleted")
        Else
           Console.WriteLine("No such row found.")
        End If
     End Sub
    To update a record, you first find the record and associate it with a DataRow object, invoke the BeginEdit method (which reminds me I need to correct a bunch of code I have that doesn't do this properly) and make your changes, then invoke EndEdit for that row.

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdatadatarowclassbeginedittopic.htm

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    If you are deleting rows from you DB, use the delete method on the datarow instead of the remove method. If you use the remove method, you will remove the row from the datatable but when you update the DB the row will still be there. Using the delete method flags the row as a pending deletion, so when you update the DB it will get deleted.

  4. #4
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    That's wierd, I haven't had problems with Remove - but then I am used to using disconnected datasets so I guess that's why. Good info.

Posting Permissions

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



Click Here to Expand Forum to Full Width