Smithbr Posted July 11, 2003 Posted July 11, 2003 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 today's 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. 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 : If dr.Item("Paid") = "" Then Quote
*Experts* Nerseus Posted July 11, 2003 *Experts* Posted July 11, 2003 If the field has never been initialized, regardless of the DataType (string in your case though), it won't 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: 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: 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 Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Smithbr Posted July 11, 2003 Author Posted July 11, 2003 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: If dr.Item("Paid") Is System.DBNull.Value Then Thanks again Nerseus! Quote
*Experts* Nerseus Posted July 11, 2003 *Experts* Posted July 11, 2003 Ah, I keep forgetting that. You can do a direct compare in C#, but not in VB. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.