locked
LINQ To Dataset DBNull problem

    Question

  • I've got a connection to a Firebird Database.
    I've linked DataSources in the IDE. I've got all my 8 tables from which I can retrieve data.

    For each table, it was automatically created a DataTable object and a TableAdapter + a main TableAdapterManager.

    At the beginning of the code I fill tables through the command:

    TableAdapter1.Fill(DataTable1);
    TableAdapter2.Fill(DataTable2);
    TableAdapter3.Fill(DataTable3);
    TableAdapter4.Fill(DataTable4);

    ecc.

    Then I created a few LINQ queries that work perfectly and give data back.
    [These queries didn't deal with Null or DBNull values].

    Finally, I wanted to use this code:

    var query = from tuple in DataTable1
                where tuple.DATETIME1 != null
                select tuple;

    foreach (var intr in query6)
            {
                Console.WriteLine("{0}", tuple.FIELD1);
               
    Console.WriteLine("{0}", tuple.FIELD2);
            }

    But when I try this, I always receive a StrongTypingException exception:

    System.Data.StrongTypingException was unhandled
      Message="The value for column '
    DATETIME1' in table 'DataTable1' is DBNull."
      Source="MyProgram"

    DATETIME1 is a field with
    AllowDBNull = True
    DataType = System.DateTime
    DefaultValue = <DBNull>
    NullValue = (Throw exception)
    Unique = false

    I don't know what to do. Does anybody can help me. I'm obviously a newbie in LINQ.
    Tuesday, April 22, 2008 7:38 PM

Answers

  • this you try this

    var query = from tuple in DataTable1
                where tuple.IsNull("DATETIME1") == false
                select tuple;

    or this

    var query = DataTable1.AsEnumerable()

        .Where(dr => dr.Field<datetime>("DATETIME1") == null)
        .Select(dr => dr);

    Or this

    var query = from dataRow in DataTable1..AsEnumerable()

                where
                    dataRow.IsNull("DATETIME1") == false
     select tuple;

    or this

    var query = from tuple in DataTable1
                where tuple.DATETIME1 != dbnull.value
                select tuple;

    check this site for some help

    Pedro Rainho
    Tuesday, April 22, 2008 8:35 PM