change field value in Datarow

  • Thread starter Thread starter cj
  • Start date Start date
C

cj

If I'm sitting on a datarow for a customer and want to change his phone
number only if it's blank what would I write?

if myDr("phone")= "" then
myDr("phone") = "mphone"
endif
mydr.acceptchanges()
 
cj said:
If I'm sitting on a datarow for a customer and want to change his
phone number only if it's blank what would I write?

if myDr("phone")= "" then
myDr("phone") = "mphone"
endif
mydr.acceptchanges()

First you should enable Option Strict and consider using typed datasets.
Yes, if you define "blank" being an empty string, this is possible. If you
mean a Null value, you can write if myDr("phone") is dbnull.value then
Acceptchanges should not be called because it resets the rowstate and
consecutive updates to the database won't be made.


Armin
 
cj said:
If I'm sitting on a datarow for a customer and want to change his phone
number only if it's blank what would I write?

if myDr("phone")= "" then


You may want to compare the value to 'DBNull.Value':

\\\
If MyDr("phone") Is DBNull.Value Then
...
End If
///

In addition, note that there is a 'IsDBNull' function available too.
 
ok but I was more concerned that mydr("phone") = mphone wasn't the
proper way to assign a variable to a field.

Armin, in regards to your comment about mydr.acceptchanges()
once I've changed a few records in the datatable I want these changes to
be reflected in the datasource. How do I accomplish that?
 
cj said:
ok but I was more concerned that mydr("phone") = mphone wasn't the
proper way to assign a variable to a field.

Armin, in regards to your comment about mydr.acceptchanges()
once I've changed a few records in the datatable I want these
changes to be reflected in the datasource. How do I accomplish
that?

It's a topic discussed in the documentation:

http://msdn2.microsoft.com/en-us/library/ss7fbaez.aspx

in particular:
http://msdn2.microsoft.com/en-us/library/bw0db3d9.aspx


Armin
 
Hi Cj,
once I've changed a few records in the datatable I want these changes to
be reflected in the datasource. How do I accomplish that?

Once you change a record in a DataTable, the changes are reflected in the
DataTable.

For example, we have a DataTable and retrieve data from a table in the
database and fill the data into the DataTable using SqlDataAdapter object.
All the rows in the DataTable have the RowState property of value Unchanged.

Then we make some changes to the first row in the DataTable and the changes
are there in the DataTable. But the value of the RowState property of the
first row becomes Modified.

You can call the GetChanges method on the DataTable to get a copy of the
DataTable containing all changes made to it and save the resulting
DataTable to the database. After you save the changes to the database
successfully, you can call the AcceptChanges method on the previous
DataTable to commit all the changes made to the table and then the value of
the RowState property of the first row becomes Unchanges again.

Hope I make some clarifications.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
cj.

As far as I know does the dataadapter only update rows which have a rowstate
other then unchanged, so the benefit is not much, to collect them first by
checking first if it haschanges (only in a dataset) and then getchanges as
it is by instance Linda describes.

The dataadapter will with do automaticly the acceptchanges (set as changes
done to the database) if you use direct the table.

Cor
 
Back
Top