0

I am populating a dataGridView from an incomplete excel spreadsheet and generating the LiteDB from the dataGridView. Before the recent update, I wasn't having any issues. Now I am getting the error 'Objects cannot be cast from DBNull to other types'. I can work around this by including dummy values in the original spreadsheet. But ultimately we need to see what information we're missing. How can I accomplish this?

    public void CreateDatabase()
    {
        using (var db = new LiteDatabase(@"C:\Users\BThatcher\Documents\_Personal\Revit\MDL-Sheets\SheetDatabase04.db")) //< ----------------
        {
            var sheets = db.GetCollection<Sheet>("sheets");

            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                var sheet = new Sheet
                {
                    SheetSequence = Convert.ToInt32(row.Cells[0].Value),
                    SheetNumber = row.Cells[1].Value.ToString(),
                    SheetName = row.Cells[2].Value.ToString(),
                    AreaNumber = Convert.ToInt32(row.Cells[3].Value),
                    AreaName = row.Cells[4].Value.ToString(),
                    SheetDiscipline = row.Cells[5].Value.ToString(),
                    DisciplineSort = Convert.ToInt32(row.Cells[6].Value),
                    SheetSort = Convert.ToInt32(row.Cells[7].Value),
                    ModelName = row.Cells[8].Value.ToString(),
                    AppearsInSheetlist = row.Cells[9].Value.ToString(),
                    DesignedBy = row.Cells[10].Value.ToString(),
                    DrawnBy = row.Cells[11].Value.ToString(),
                    CheckedBy = row.Cells[12].Value.ToString(),
                    ApprovedBy = row.Cells[13].Value.ToString(),
                    SheetTotal = Convert.ToInt32(row.Cells[14].Value),
                    DesignSoftware = row.Cells[15].Value.ToString(),
                    HasPdf = row.Cells[16].Value.ToString(),
                    PdfPath = row.Cells[17].Value.ToString(),                        
                };
                sheets.Insert(sheet);                    

                this.Close();
            }
        }
    }
Hamed
  • 5,867
  • 4
  • 32
  • 56
topofsteel
  • 1,267
  • 6
  • 16
  • 25

1 Answers1

0

This error are not from LiteDB (there is no DBNull in LiteDB). It's possible this DBNull are getting from row.Cells[n].Value when you try to convert.

LiteDB support nulls and nullable values (like int?).

mbdavid
  • 1,076
  • 7
  • 8