Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dataadapter Update problem

Status
Not open for further replies.

astrodestino

IS-IT--Management
Feb 19, 2005
179
AR
Hi!
Thanks to SqeakingSweep I could manage to force to change everynull date value to date.now.
I'm not used to work with dataset and datareaders and datagrids but this time I got to use them and I need a hand.
This datagrid thing is a small (very small) part of the project and I cant skip it :(
I got this code:
Code:
 Try
            Dim SQLString As String, connstring As String
            connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & nombrebase & ";Persist Security Info=False"
            SQLString = "Select * From " & tabla2
            Dim DBDataAdapter As New OleDb.OleDbDataAdapter(SQLString, connstring)
            Dim DBUpdateCommand As New OleDb.OleDbCommandBuilder(DBDataAdapter)
            If mfecha <> "" Then
                For Each r As DataRow In ds.Tables(0).Rows
                    ''MsgBox(mfecha)
                    If IsDBNull(r(mfecha)) Then r(mfecha) = Date.Now
                Next
            End If
            DBDataAdapter.Update(ds, tabla2)
            Me.Close()
        Catch Ex As Exception
            MsgBox(Ex.Message)
        End Try
Here the app will check for null date column values and replace them with date.now write all the datagrid changes to the database and will close the form but I got a problem, sometimes, when I delete one row it will rise an error here:

For Each r As DataRow In ds.Tables(0).Rows
If IsDBNull(r(mfecha)) Then r(mfecha) = Date.Now <- Error Here
Next
mfecha is date field name

And the error says:(translated)
Cant have access to a deleted row information

My database has not required fields and it has 4 fields:
ID, text, binary & date. The only null field is Binary.

Is there anyway that can be fixed? It seems that it will check the deleted row too!

Thank you!!!!!
 
Just check the RowState of each row before you try to access it:

For r As Integer = 0 To ds.Tables(0).Rows.Count - 1
If ds.Tables(0).Rows(r).RowState <> DataRowState.Deleted Then
If IsDBNull(ds.Tables(0).Rows(r).Item(mfecha)) Then ds.Tables(0).Rows(r).Item(mfecha) = Date.Now
End If
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top