Urgent!! Datagrid Column update problem

Smithbr

Member
Joined
Jun 3, 2003
Messages
15
Hello, I am having a real problem figuring out why this code is not working in my vb.net project. I am tryig to get todays date, which is dispalyed in lbltdatedisp to insert into the paid column only if teh field is blank...in other words, i want to be able to update it and mark the date on day...go through and add a few more records, and mark those with a different date withut changing the date of those mark previously. In my access database the paid column is set as text so I did not think it whoud be a problem comparing it via ="" ( as you will see below in my code). Can anyone help me out with this... really need to have this working by the end of the day and can not see what I need to do...Thanks in advance for everything.

Code:
 Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click

        If DsInvoice1.HasChanges Then
            If MessageBox.Show("You are about to save all changes made in the Datagrid, Continue?", "InvoiceHistory", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
                Da.Update(detail, "InvoiceHistory")
                Try
                    Dim dr As DataRow
                    For Each dr In DsInvoice1.Tables(0).Rows
                        If dr.Item("Paid") = "" Then
                            dr.Item("Paid") = lbltdatedisp.Text
                        End If

                    Next
                    OleDbDataAdapter1.Update(DsInvoice1)


                    grdComm.Refresh()
                Catch Ex As OleDbException
                    System.Diagnostics.Debug.Write(Ex.Message)
                End Try
            End If
        End If
End Sub


the error message i keep getting is An unhandled exception of type System.InvalidCastException occurred in microsoft.visualbasic.dll

Additional information: Operator is not valid for type DBNull and string "".
and it is referencing the line :
Code:
    If dr.Item("Paid") = "" Then
 
If the field has never been initialized, regardless of the DataType (string in your case though), it wont be a default type (not "" for strings, not 0 for ints). Instead it will be a special NULL value, called System.DBNull.Value. Try this instead:

Code:
If dr.Item("Paid") = System.DBNull.Value Then
    dr.Item("Paid") = lbltdatedisp.Text
End If

If you think you may have put "" in some of the records, use this:
Code:
If dr.Item("Paid") = System.DBNull.Value Then
    dr.Item("Paid") = lbltdatedisp.Text
ElseIf dr.Item("Paid") = String.Empty Then
    dr.Item("Paid") = lbltdatedisp.Text
End If

I only changed your "" to String.Empty for the second IF.

-Nerseus
 
Thank you so much...I have been trying to figure this out for way longer than I wan to admitt....also, for anyone in the future,
The code is actually slighty differnet then above if it is going to work. It is:
Code:
If dr.Item("Paid") Is System.DBNull.Value Then
Thanks again Nerseus!
 
Back
Top