just want to create a row in my data set

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
I'd like to bring all this technical talk DOWN a notch by asking:

I have an sql table called OBAdmit
I have a FormsDataSet.xsd that has been created with this table.

I use TableAdapter with .fill and .update methods to get info back and forth from SQL to my .NET forms. I also use the .insert method at times.

But here's what I WANT to do...

Let's say I want to add a row to my data set, but I don't want it to go to SQL just yet (never mind why, I just have to do it that way!)

I have a search menu that is populated using a .fill, and it gives me a set of records currently in SQL...then I select one, and open up my form. I then use a .fillbyAdmitID that only retrieves one row, based on which record I selected, and then I populate my form controls (not by binding, but rather by code.)

Now, let's say I select a record from this search menu and want to create ANOTHER record, where some of the info from this record flows over to the new record, but I don't want that record in SQL just yet. I just want to create a row in my data set and then open up my form and have the controls populated by some of the data from the record I selected? HOW, in the name of.... do I do this?

I can't use .fill or .update or even .insert because this sends the data over to SQL. I dont' want to do that yet.
 
use AcceptChanges

Hi

You can call the AcceptChanges method of the newly added datarow immediately. This sets the datarow's RowState to "Unchanged".

Dataadapter decides and performs any action on the dataset rows based on their RowState. If the rowstate is "Unchanged", dataadapter does not consider those rows for any manipulation.

Calling Acceptchanges on just a single row, does not affect the state of other rows and hence the other rows are safely updated.

Sample code is given below.

Dim
dr As DataRow
dr = ds.tables(0).rows.newrow
dr.Item(0) = ""
dr.Item(1) = ""
ds.tables(0).Rows.Add(dr)
dr.AcceptChanges()


Hope this helps.
 
One more Tip

One more tip:

if you want to remove a row from the datatable/dataset, but not from the database, u can use Remove method of the datatable.

In contrast to the Delete method, Remove does not delete rows from the database.
 
Hi

I have a dataTable called OrderDetails which contains order lines for an order. The order lines are shown in a flex grid, unbound.I want to be able to allow the user to select an order line and delete it. This does not update the database at this point, it should only do a .update when the user presses save.

If I use .remove on the datarow and repopulate my flexgrid the record has disappeared. If the user then presses save and i do the .update the record DOESNT get deleted from the database.

If I do a .delete on the datarow then my code to repopulate the grid fails as the datatable still contains 2 records and im trying to use the values of the deleted record.

What is the best solution here? Do I need to check for deleted rows when im looping through the datatable to show them or is there a better way?

Thanks

Darren
 
Last edited:
Hello again

Ive used the delete method and added an if statement to the populategrid code that checks for deleted records.

The delete statement requires an item id of the record in the datatable that i want to delete. I use the row in the flex grid. However if I delete row 1 and then row 1 again (which was row 2) I get an obvious error as im trying to delete somehting that has already been deleted.

So is delete still better than remove? How do i delete the right datarow from the table?

e.g.

datatable flex grid
1 1
2 x 4
3 x 5
4
5

If I delete id 5 row 3 from flexgrid then im trying to delete a record already deleted. I cant even loop through until i find a non-deleted record as that would remove id 4.

Do I need to add a new column to the flexgrid that stores the row number each record is within the datatable and use this for deletion purposes?

Thanks

Darren
 
Delete is used to mark a row as deleted so that it will be removed from the database next time .Update() is called. .Deleted rows also do not appear in data grids etc on the client side because the grid is programmed not to show them.

Remove is used to remove a row from a data table - the database row that is relevant to this row will not be altered. Removed rows do not appear in data grids on the client side because they are not there any more.


Decide which you want to use.. Do you want to update the database or not?
There is no provision for showing a record that will be deleted. In this case you must manage this yourself with flags, and iterate the dataset before you update, setting all rows whose "MyDeleteFlag" column is True, to Deleted
 
Hi

Thanks for the reply CJard !

I used the delete method on the row as I do need to delete the record from the database. When iterating the datarows within the datatable I check the deleted status before using the datatrow. This is so the user can still press Save or Cancel to commit the changes or to leave the state of the record in the db unchanged.

Cheers

D
 
Im not 100% sure how there is the need to iterate the dataset.. If the users presses Cancel, then dont call Update()

You can, additionally, have the tableadapter only update certain kinds of rows:

Dim tempDT as DataTable = myDataTable.GetChanges(RowState.Added Or RowState.Modified)

'tempDT now contains rows that were added or updted, but not deleted
 
Back
Top