Empty field in Access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joachim

    Empty field in Access

    Hi

    I am a beginner in VB.NET, and have a problem with empty field in Access

    I have transfered a worksheet in Excel to Access table. Some of the cels are empty
    I use VB.NET program to acces this Access table (product.mdb)

    One of the statements is to validate whether the field empty or not
    My statement is
    If Trim(dr_Product ("bt_m3event ")) = "" Or Trim(dr_Product ("bt_m3event ")) = " " The
    ........
    End I

    dr_Product ==> is OleDbDataReade
    bt_m3event ==>is the field nam

    The bt_m3event is empty
    When this statement is processed, I got a message like below

    An unhandled exception of type 'System.Invalid CastException' occurred in microsoft.visua lbasic.dl
    Additional information: Cast from type 'DBNull' to type 'String' is not valid

    I have tried so many times and many ways, but it didn't work
    Can anybody tell me how to validate an empty field

    Thanks in advance

    Joachim

  • Armin Zingler

    #2
    Re: Empty field in Access

    "Joachim" <anonymous@disc ussions.microso ft.com> schrieb[color=blue]
    > Hi,
    >
    > I am a beginner in VB.NET, and have a problem with empty field in
    > Access.
    >
    > I have transfered a worksheet in Excel to Access table. Some of the
    > cels are empty. I use VB.NET program to acces this Access table
    > (product.mdb).
    >
    > One of the statements is to validate whether the field empty or
    > not. My statement is :
    > If Trim(dr_Product ("bt_m3event ")) = "" Or
    > Trim(dr_Product ("bt_m3event ")) = " " Then
    > .........
    > End If
    >
    > dr_Product ==> is OleDbDataReader
    > bt_m3event ==>is the field name
    >
    > The bt_m3event is empty.
    > When this statement is processed, I got a message like below :
    >
    > An unhandled exception of type 'System.Invalid CastException' occurred
    > in microsoft.visua lbasic.dll Additional information: Cast from type
    > 'DBNull' to type 'String' is not valid.
    >
    > I have tried so many times and many ways, but it didn't work.
    > Can anybody tell me how to validate an empty field ?[/color]


    First, you should enable Option Strict. Your code can not be compiled here
    because the type of the DataReader's Item property is Object. You can not
    compare every object to a string and you can't pass it to the Trim function.

    As the exception message says, the field contains a Null value. The only
    instance of Null in the Framework is DBNull.Value:

    If dr_Product("bt_ m3event") Is DBNull.Value Then
    msgbox "Null found"
    else
    dim FieldContent as string
    FieldContent = dr_Product("bt_ m3event").ToStr ing
    end if

    For all other date types, you must use type casting:

    If dr_Product("Any IntegerField") Is DBNull.Value Then
    msgbox "Null found"
    else
    dim FieldContent as Integer
    FieldContent = Directcast(dr_P roduct("bt_m3ev ent"), Integer)
    end if


    --
    Armin

    How to quote and why:



    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Empty field in Access

      * "=?Utf-8?B?Sm9hY2hpbQ= =?=" <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
      > I have transfered a worksheet in Excel to Access table. Some of the cels are empty.
      > I use VB.NET program to acces this Access table (product.mdb).
      >
      > One of the statements is to validate whether the field empty or not.
      > My statement is :
      > If Trim(dr_Product ("bt_m3event ")) = "" Or Trim(dr_Product ("bt_m3event ")) = " " Then[/color]

      'If dr_Product("bt_ m3event") Is DBNull.Value Then...'. The right part of
      the 'Or' IMO doesn't make sense at all because 'Trim' will remove the spaces.

      --
      Herfried K. Wagner [MVP]
      <URL:http://dotnet.mvps.org/>

      Comment

      • Joachim

        #4
        RE: Empty field in Access

        Armin and Herfried
        Thanks to you. It works
        But I have another problem. I have posted it in the other thread
        I think you can help me out.

        Comment

        Working...