Casting a DBNull

Discussion in 'ASP .Net' started by .Net Sports, Jan 25, 2006.

  1. .Net Sports

    .Net Sports Guest

    I'm trying to match fields from XML feed to a datarow, but I get a
    "System.InvalidCastException: Object cannot be cast from DBNull to
    other types" error at the PkgAmount line. This code was converted from
    original C# code, so I'm wondering if it has something to do with C#'s
    strict datatype criteria (even tho this is in vb.net), or I need to do
    something extra around the
    FormatAmount(Convert.ToSingle(drwData("PkgAmount"))) ...
    '''''''''''''''''
    Dim drwData As DataRow
    For Each drwData In dtaData.Rows
    Dim SID As String = "SID=" & Convert.ToString(drwData("ID")) & "&"
    Dim PurchaseTypeID As String = Convert.ToString(drwData("PurchaseID"))
    Dim PkgAmount As String =
    FormatAmount(Convert.ToSingle(drwData("PkgAmount")))
    '''''''''''''''''''''
    TIA
    netsports
     
    .Net Sports, Jan 25, 2006
    #1
    1. Advertising

  2. Hi

    Why not write it in VB.Net code (However, you can in C# as well not use a
    string as a single)

    Altough it is a little bit strange code, alone the last line will fill the
    items which are inside the method so those can never be used.

    Dim drwData As DataRow
    For Each drwData In dtaData.Rows
    Dim SID As String = SID=" & drwData("ID").ToString & "&"
    Dim PurchaseTypeID As String = drwData("PurchaseID").ToString
    Dim PkgAmount As Single = Csng(drwData("PkgAmount"))
    ''Or just as the other strings,

    I hope this helps,

    Cor
     
    Cor Ligthert [MVP], Jan 25, 2006
    #2
    1. Advertising

  3. I am not sure if this helps but in C# if you have a nullable value say a
    birthday that can be read from db then you would use


    DateTime? fBirthday;

    // read from db
    ...

    // save as string

    String tmp = ""

    if ( fBirthday != null )
    {
    tmp = ( (DateTime) fBirthday ).Date;
    }

    Sorry but I don't know vb, but I have to do similiar things for columns that
    can be null in some tables.
     
    David Hubbard, Jan 25, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravikanth[MVP]
    Replies:
    6
    Views:
    3,917
    Aemca
    Jul 18, 2003
  2. Boris Zakharin

    Adding a DBNull value to the database

    Boris Zakharin, Aug 27, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    402
    Boris Zakharin
    Aug 27, 2003
  3. kevin
    Replies:
    11
    Views:
    5,834
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    540
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    587
    James Kanze
    Dec 20, 2009
Loading...

Share This Page