c# - System.DBNull to String error in excel -
i have own custom file type similar .csv except custom delimiters. delimiter comma (char)20 (looks square) , quotes (char)254 (looks þ). have created excel 2010 add-in in visual studio parse through document replaces custom delimiters commas , double-quotes in .csv format. 
the program creates new toolbar , button start process. works fine on documents not on others , if try twice in 1 instance of excel comes  error "cannot implicitly convert type 'system.dbnull' 'string'". because row.text property being read {}.
now question causing row.text property read {} instead of text inside cell? why occur on documents not others though use same encoding? 
an example of in cell (note-the comma symbol won't print here):
þitem_idþþbegdocþþenddocþþbegattþþendattþþparent_attachmentþþattachment_batesþþ etc.   edit here code:
    public partial class thisaddin {     office.commandbarbutton toolbarcommand;      private void thisaddin_startup(object sender, system.eventargs e)     {         office.commandbar toolbar = application.commandbars.add("my toolbar",office.msobarposition.msobartop,false,true);         toolbarcommand = (office.commandbarbutton)         toolbar.controls.add(            office.msocontroltype.msocontrolbutton,            missing,            missing,            missing,            true);         toolbarcommand.caption = "toolbar button";         toolbarcommand.faceid = 59;         toolbarcommand.click += new office._commandbarbuttonevents_clickeventhandler(toolbarcommand_click);         toolbar.visible = true;     }        void toolbarcommand_click(office.commandbarbutton ctrl, ref bool canceldefault)     {         excel.worksheet activeworksheet = ((excel.worksheet)application.activesheet);         try         {             iteraterows(activeworksheet);         }         catch(exception e)         {             messagebox.show(e.tostring());         }         ctrl.click+=new office._commandbarbuttonevents_clickeventhandler(toolbarcommand_click);     }      public void iteraterows(excel.worksheet worksheet)     {         //get used range         excel.range usedrange = worksheet.usedrange;          excel.worksheet activeworksheet = ((excel.worksheet)application.activesheet);          //iterate rows in used range          if (usedrange.rows.count > 1)         {             foreach (excel.range row in usedrange.rows)             {                 //messagebox.show(row.text);                 char quote = (char)254;                 string data = row.text;                 row.columns[1] = data.replace(quote, '"').replace((char)20, ',');                 row.texttocolumns(type.missing, excel.xltextparsingtype.xldelimited, excel.xltextqualifier.xltextqualifierdoublequote, type.missing, type.missing, type.missing, true);             }         }     }       
 
Comments
Post a Comment