Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

DBNULL TO INTEGER EXCEPTION

Last post 03-23-2008 8:50 PM by qüeb.cesar. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-20-2008 6:02 PM

    • blaknite
    • Top 500 Contributor
    • Joined on 12-01-2003
    • Addicted Member
    • Points 925

    DBNULL TO INTEGER EXCEPTION

            Conversion from type 'DBNull' to type 'Integer' is not valid.
            Dim mylevel As Integer
            Dim EXPGained As Integer
            Dim EXPNeeded As Integer
            Dim EXPPercentage As Decimal
            AdaraConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\EXP.mdb"
            AdaraDataSet.Clear()
            AdaraDataAdapter.SelectCommand.CommandText = "SELECT * FROM EXP"
            AdaraDataAdapter.Fill(AdaraDataSet)
            AdaraDataTable = AdaraDataSet.Tables(0)
            ExpToolStripStatusLabel.Text = "Exp: " & EXPPercentage & "%"
            mylevel = AdaraDataTable.Rows(0)(0) <----- ERROR OCCURS HERE ---->
            AdaraConnection.Close()
    

     

    why is it when I try to retrieve a number from my database I am getting this error? the number is not 0 or blank....

    • Post Points: 10
  • 03-23-2008 8:50 PM In reply to

    Re: DBNULL TO INTEGER EXCEPTION

    The column in the zero position is returning a DBNull value. This main that the column hasn't a value.

    You must to check if the returned value is not DBNull (previouslly, you must to check that the dataset, table collections and rows collections are not empty or nothing.

    If AdaraDataSet IsNot Nothing Then

       If AdaraDataSet.Tables IsNot Nothing Then

        ...... check the rows collection

               If Not DbNull.Value.Equals(AdaraDataTable.Rows(0)(0)) Then

                  'Assign the value

                   mylevel = Convert.ToInt32(AdaraDataTable.Rows(0)(0))

             ..... more code here

    Check the Convert class and tje DBNull.Value evaluation.

    Regards

     

     
    • Post Points: 5
Page 1 of 1 (2 items)