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.
Printable View
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.
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):
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: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)
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdatadatarowcollectionclassremovetopic.htm
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.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
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemdatadatarowclassbeginedittopic.htm
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.
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.