|
-
May 7th, 2003, 08:25 PM
#1
Thread Starter
Junior Member
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.
-
May 7th, 2003, 08:58 PM
#2
Fanatic Member
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
-
May 7th, 2003, 10:32 PM
#3
Frenzied Member
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.
-
May 8th, 2003, 06:33 AM
#4
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|