Results 1 to 7 of 7

Thread: [RESOLVED] Checking database field

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Resolved [RESOLVED] Checking database field

    I'm trying to populate a custom data table with info from some tables in a dataset, the problem is I can't seem to check if the field is empty (if it is I just want to pass over it and grab the next row).

    This is the code I'm using but it just doesn't function as I'd expected.
    VB Code:
    1. For rowCount=1 to objDataSet.Tables("medical").rows.count
    2.         If objDataSet.Tables("medical").rows(rowCount-1).Item(condition) IS Nothing then
    3.             expireDate=""
    4.         ELSE
    5.             expireDate=objDataSet.Tables("medical").rows(rowCount-1).Item(condition)
    6.         End If
    7.  
    8.         if expireDate >= dateToday then
    9.             strName=objDataSet.Tables("medical").rows(rowCount-1).Item("FirstName") & " " & objDataSet.Tables("medical").rows(rowCount-1).Item("LastName")
    10.             objDataRow=dtblExpired.NewRow()
    11.             objDataRow.Item("Name")= strName
    12.             dtblExpired.Rows.Add(objDataRow)
    13.         End If
    14.     Next

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Checking database field

    try
    objDataSet.Tables("medical").rows(rowCount-1).Item(condition) = Nothing then

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Checking database field

    or
    objDataSet.Tables("medical").rows(rowCount-1)("condition") = Nothing

    i assume "condition" is a column name

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Checking database field

    A null field in a DataRow is not equal to Nothing. It is equal to DBNull.Value. Also, if "expireDate" is a Date variable then why are you setting it to an empty string? How about this for some slighly neater code:
    VB Code:
    1. For Each row As DataRow In objDataSet.Tables("medical").Rows
    2.     If row(condition) <> DBNull.Value AndAlso CDate(row(condition)) >= Date.Today Then
    3.         objDataRow = dtblExpired.NewRow()
    4.         objDataRow("Name") = row("FirstName") & " " & row("LastName")
    5.         dtblExpired.Rows.Add(objDataRow)
    6.     End If
    7. Next row
    This code is easier to read and more efficient because the number of property accesses and method calls is smaller. Also, the overuse of local variables can often make code harder to understand rather than easier.
    Last edited by jmcilhinney; Aug 22nd, 2005 at 08:43 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Checking database field

    Thanks for the help on this, I'm new to VB.NET and I'm starting to look at my code in a different light

    However I'm getting this error message with your code jmcilhinney, can you take a look and advise me please.

    BC31080: Operator '<>' is not defined for types 'System.Object' and 'System.DBNull'. Use 'Is' operator to compare two reference types.

    Source Error:

    Line 84: For Each row As DataRow In objDataSet.Tables("medical").Rows
    Line 85: If row(condition) <> DBNull.Value AndAlso CDate(row(condition)) >= Date.Today Then
    Line 86: objDataRow = dtblExpired.NewRow()
    Line 87: objDataRow("Name") = row("FirstName") & " " & row("LastName")

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Checking database field

    Sorry. Change:
    VB Code:
    1. If row(condition) <> DBNull.Value AndAlso CDate(row(condition)) >= Date.Today Then
    to
    VB Code:
    1. If Not row(condition) Is DBNull.Value AndAlso CDate(row(condition)) >= Date.Today Then
    I think that should do the trick. One of the hazards of typing code into a post directly rather than copying from the IDE.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: Checking database field

    Thanks for your time on this, it's really appreciated, it works a treat and I've learned alot about my code, your solution is so much slicker

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width