visual studio 2013 - Error "index was out of range. Must be non-negative and less than the size of the collection" C# WPF vs2013 -


i have problem: c# wpf (vs 2013 pro, .net framework 4.0) standalone desktop application works on windows operating systems 1 exception: on windows xp error:

"index out of range. must non-negative , less size of collection".

the error comes out on datagrid (event mousedoubleclick or contextmenu), in tabcontrol (there 26 tabs, letters in latin alphabet (a, b, c....)) when choose record modify datagrid , open form data record (like formname.showdialog()). strange thing me numbers ok, mean index 0 (the first record of datagrid) , real id of record ok.

this code (the error on showdialog); tried insert try catch around showdialog() see happens , got error.

 private void datagridelencopazienti_mousedoubleclick(object sender,    mousebuttoneventargs e)     {         persona paziente = getrigapaz();          if (paziente == null)         {             messagebox.show("prova selezionare la riga cliccando col pulsante sinistro e poi col destro e quindi selezionare l'opzione desiderata !", "selezione riga", messageboxbutton.ok, messageboximage.exclamation);             return;         }          formcartellapaziente formcartellapaz = new formcartellapaziente(datiutente, idazienda, paziente, this);          this.hide();         formcartellapaz.showintaskbar = true;         //this.showintaskbar = false;         formcartellapaz.showdialog();      }    private persona getrigapaz()     {         int idpaziente = 0;         datarow datarow = null;         datarowview pazientesel = null;         persona paziente = null;          if (datagridelencopazienti.selecteditem == null)         {             //messagebox.show("prova selezionare la riga cliccando col pulsante sinistro e poi col destro e quindi selezionare l'opzione desiderata !", "selezione riga", messageboxbutton.ok, messageboximage.exclamation);             return null;         }          pazientesel = (datarowview) datagridelencopazienti.selecteditem;          try         {             datarow = pazientesel.row;             idpaziente = convert.toint16(datarow["id"].tostring());              paziente = new persona();             paziente = personadao.read(idpaziente, 0);          }         catch (exception ex)         {             messagebox.show("errore selezione paz - formcasellariopazienti: 309 + ---" + ex.tostring());         }          return paziente;     } 

thank adrian

is possible there no column id? exception should triggered following line:

idpaziente = convert.toint16(datarow["id"].tostring()); 

i suggest check if column exists , has value convert int16 type.

btw, don't have tostring() before convert.toint16. without..


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -