Typed DataSet: "Cannot get value because it is DBNull": Why still using it?

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

    Typed DataSet: "Cannot get value because it is DBNull": Why still using it?

    Hi,

    Something I don't understand about a Typed DataSet: When a value in the
    DataSet is DBNull, it throws this error: "Cannot get value because it is
    DBNull".

    But aren't Typed DataSets invented to make life easier, to be able to get to
    tge Tables and Values with less code, in less time? But with this thing you
    need to add a Try-Catch around every statement when using the value, add for
    each value a default value in your DataSet (and you have to redo it each
    time something changes to your DataSet!), or don't allow Null-values in any
    of your Tables in your Sql Server..

    So why are people still using these things? Did anybody find an easy, fast
    and good working solution to get around this problem?

    Pieter


  • cody

    #2
    Re: Typed DataSet: "Cannot get value because it is DBNull": Why still using it?

    If the value in the dataset is null what do you expect the getter to return?
    How do you expect they will cast DBNull in, say, int? If you do not
    need/want nulls give the DataColumn a defaultvalue and set allowdbnull to
    false.
    You can also use the untyped indexer of the datarow which will return what
    is in the row regardless which type and wheather it is null or not.

    And one important thing: please do not crosspost, this is inpolite.

    Hope it helps.


    "DraguVaso" <pietercoucke@h otmail.com> schrieb im Newsbeitrag
    news:#DtHbGHiFH A.3700@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi,
    >
    > Something I don't understand about a Typed DataSet: When a value in the
    > DataSet is DBNull, it throws this error: "Cannot get value because it is
    > DBNull".
    >
    > But aren't Typed DataSets invented to make life easier, to be able to get[/color]
    to[color=blue]
    > tge Tables and Values with less code, in less time? But with this thing[/color]
    you[color=blue]
    > need to add a Try-Catch around every statement when using the value, add[/color]
    for[color=blue]
    > each value a default value in your DataSet (and you have to redo it each
    > time something changes to your DataSet!), or don't allow Null-values in[/color]
    any[color=blue]
    > of your Tables in your Sql Server..
    >
    > So why are people still using these things? Did anybody find an easy, fast
    > and good working solution to get around this problem?
    >
    > Pieter
    >
    >[/color]


    Comment

    • DraguVaso

      #3
      Re: Typed DataSet: &quot;Cannot get value because it is DBNull&quot;: Why still using it?

      Well, I expect it to return DBNull, and not an error! DBNull is actually
      also some kind of value, so throwing an exception isn't really a nice thing
      in my opinion, hehe :-) When using the Untyped DataSet it returns DBNull
      too, so i don't see a reason why they have changed this with a Typed
      DataSet...

      And yes indeed you can use the untyped indexer every time: but again: why
      use a Typed DataSet if you need to call everytime the untyped to read the
      value's? It jsut doesn't make any sense to me :-(

      Pieter

      PS: I didn't croospost this? I just send it to 4 newgroups that seem
      logically concerned to this: framework, adonet (it has something to do with
      it), vb (which I uses), general (because it happens in other languages too).
      But anyways my apologizes if I harmed anybody with this.

      "cody" <deutronium@gmx .de> wrote in message
      news:OROaNPHiFH A.2156@TK2MSFTN GP14.phx.gbl...[color=blue]
      > If the value in the dataset is null what do you expect the getter to[/color]
      return?[color=blue]
      > How do you expect they will cast DBNull in, say, int? If you do not
      > need/want nulls give the DataColumn a defaultvalue and set allowdbnull to
      > false.
      > You can also use the untyped indexer of the datarow which will return what
      > is in the row regardless which type and wheather it is null or not.
      >
      > And one important thing: please do not crosspost, this is inpolite.
      >
      > Hope it helps.
      >
      >
      > "DraguVaso" <pietercoucke@h otmail.com> schrieb im Newsbeitrag
      > news:#DtHbGHiFH A.3700@TK2MSFTN GP10.phx.gbl...[color=green]
      > > Hi,
      > >
      > > Something I don't understand about a Typed DataSet: When a value in the
      > > DataSet is DBNull, it throws this error: "Cannot get value because it is
      > > DBNull".
      > >
      > > But aren't Typed DataSets invented to make life easier, to be able to[/color][/color]
      get[color=blue]
      > to[color=green]
      > > tge Tables and Values with less code, in less time? But with this thing[/color]
      > you[color=green]
      > > need to add a Try-Catch around every statement when using the value, add[/color]
      > for[color=green]
      > > each value a default value in your DataSet (and you have to redo it each
      > > time something changes to your DataSet!), or don't allow Null-values in[/color]
      > any[color=green]
      > > of your Tables in your Sql Server..
      > >
      > > So why are people still using these things? Did anybody find an easy,[/color][/color]
      fast[color=blue][color=green]
      > > and good working solution to get around this problem?
      > >
      > > Pieter
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Miha Markic [MVP C#]

        #4
        Re: Typed DataSet: &quot;Cannot get value because it is DBNull&quot;: Why still using it?

        Hi Dragu,

        Every nullable column in typed dataset has an Is[ColumnName]Null() method.

        --
        Miha Markic [MVP C#] - RightHand .NET consulting & development

        Blog: http://cs.rthand.com/blogs/blog_with_righthand/
        SLODUG - Slovene Developer Users Group www.codezone-si.info

        "DraguVaso" <pietercoucke@h otmail.com> wrote in message
        news:%23DtHbGHi FHA.3700@TK2MSF TNGP10.phx.gbl. ..[color=blue]
        > Hi,
        >
        > Something I don't understand about a Typed DataSet: When a value in the
        > DataSet is DBNull, it throws this error: "Cannot get value because it is
        > DBNull".
        >
        > But aren't Typed DataSets invented to make life easier, to be able to get
        > to
        > tge Tables and Values with less code, in less time? But with this thing
        > you
        > need to add a Try-Catch around every statement when using the value, add
        > for
        > each value a default value in your DataSet (and you have to redo it each
        > time something changes to your DataSet!), or don't allow Null-values in
        > any
        > of your Tables in your Sql Server..
        >
        > So why are people still using these things? Did anybody find an easy, fast
        > and good working solution to get around this problem?
        >
        > Pieter
        >
        >[/color]


        Comment

        • DraguVaso

          #5
          Re: Typed DataSet: &quot;Cannot get value because it is DBNull&quot;: Why still using it?

          Thanks! That solves my problem I guess :-)

          "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
          news:uw0iElHiFH A.2072@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Hi Dragu,
          >
          > Every nullable column in typed dataset has an Is[ColumnName]Null() method.
          >
          > --
          > Miha Markic [MVP C#] - RightHand .NET consulting & development
          > www.rthand.com
          > Blog: http://cs.rthand.com/blogs/blog_with_righthand/
          > SLODUG - Slovene Developer Users Group www.codezone-si.info
          >
          > "DraguVaso" <pietercoucke@h otmail.com> wrote in message
          > news:%23DtHbGHi FHA.3700@TK2MSF TNGP10.phx.gbl. ..[color=green]
          > > Hi,
          > >
          > > Something I don't understand about a Typed DataSet: When a value in the
          > > DataSet is DBNull, it throws this error: "Cannot get value because it is
          > > DBNull".
          > >
          > > But aren't Typed DataSets invented to make life easier, to be able to[/color][/color]
          get[color=blue][color=green]
          > > to
          > > tge Tables and Values with less code, in less time? But with this thing
          > > you
          > > need to add a Try-Catch around every statement when using the value, add
          > > for
          > > each value a default value in your DataSet (and you have to redo it each
          > > time something changes to your DataSet!), or don't allow Null-values in
          > > any
          > > of your Tables in your Sql Server..
          > >
          > > So why are people still using these things? Did anybody find an easy,[/color][/color]
          fast[color=blue][color=green]
          > > and good working solution to get around this problem?
          > >
          > > Pieter
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Ross Presser

            #6
            Re: Typed DataSet: &quot;Cannot get value because it is DBNull&quot;: Why still using it?

            On Thu, 14 Jul 2005 15:23:36 +0200, DraguVaso wrote:
            [color=blue]
            > Well, I expect it to return DBNull, and not an error! DBNull is actually
            > also some kind of value, so throwing an exception isn't really a nice thing
            > in my opinion, hehe :-)[/color]

            DBNull is a kind of value. But it is not convertible to int, string, or
            anything else. The declaration of the properties are that they return the
            types given. An example from one of my typed datasets:

            Public Property label As String
            Get
            Try
            Return CType(Me(Me.tab leVariablepg.la belColumn),Stri ng)
            Catch e As InvalidCastExce ption
            Throw New StrongTypingExc eption( _
            "Cannot get value because it is DBNull.", e)
            End Try
            End Get
            Set
            Me(Me.tableVari ablepg.labelCol umn) = value
            End Set
            End Property


            Now, if the label property must return a string, then you can't return
            DBNull.Value. Just try it in code - the compiler won't even let you.
            Insert "return dbnull.value" below the catch statement, and watch the nice
            wiggly blue line appear underneath it.
            [color=blue]
            > When using the Untyped DataSet it returns DBNull
            > too, so i don't see a reason why they have changed this with a Typed
            > DataSet...[/color]

            When you use an untyped dataset, it returns an Object type. When you use a
            typed dataset, it returns a type. Simple.

            What are you going to do in the code when the value is null? You're going
            to do something special. So why not check IsNull() beforehand anyway?

            Comment

            Working...