sql - What's wrong with this Winform C# code? It will run fine in Windows, but in mono(Linux), will throw a NullReferenceException -
the error is:
system.nullreferenceexception: object reference not set instance of object.
i know error occurs when object used not initialize or being initialized later declared null nevertheless being accessed caused exception. can't figure out offending object can correct myself. error show after selected string listbox control second time. @ first work , if select item second time program quit abruptly, error show @ command line in linux.
i can show complete error after running mono win_binary_file
if needed.
after debugging, i've found offending code throw nullreferenceexception:
public void flistitems(datagridview datagridview1, string param_name) { npgsqldataadapter dr = default(npgsqldataadapter); dataset ds; string sql; npgsqlconnection dbcon; string connectionstr = "server=" + databasehost + ";database=" + databasename + ";user id=" + databaseuser + ";password=" + databasepassword + ";pooling=true; port=" + databaseport; dbcon = new npgsqlconnection(connectionstr); try { sql = "select * purchase_order" + " purchase_request_num= '" + param_name + "' order id asc; "; dbcon.open(); ds = new dataset("purchase_order"); dr = new npgsqldataadapter(); dr.selectcommand = new npgsqlcommand(sql, dbcon); dr.fill(ds, "purchase_order"); datagridview1.datasource = ds.tables["purchase_order"]; } catch (applicationexception ex) { messagebox.show(ex.message); } { dbcon.close(); } }
this code call function above:
private void listboxpr_selectedindexchanged(object sender, eventargs e) { cursor = cursors.waitcursor; string prnumberselect; if (varname == 180) { //somecode here condition varname=180 prnumberselect = listboxpr.text.tostring(); class1.flistitems(datagridrequests, prnumberselect); } else { prnumberselect = listboxpr.text.tostring(); class1.flistitems(datagridrequests, prnumberselect); } cursor = cursors.default; }
the complete error:
system.nullreferenceexception: object reference not set instance of object @ purchase_requisition.frmmain.datagridrequests_selectionchanged (system.object sender, system.eventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.onselectionchanged (system.eventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.setselectedrowcore (int32 rowindex, boolean selected) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.setselectedrowcoreinternal (int32 rowindex, boolean selected) [0x00000] in <filename unknown>:0 @ (wrapper remoting-invoke-with-check) system.windows.forms.datagridview:setselectedrowcoreinternal (int,bool) @ system.windows.forms.datagridviewband.set_selected (boolean value) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridviewrow.set_selected (boolean value) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.clearselection () [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.movecurrentcell (int32 x, int32 y, boolean select, boolean iscontrol, boolean isshift, boolean scroll) [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.clearbinding () [0x00000] in <filename unknown>:0 @ system.windows.forms.datagridview.set_datasource (system.object value) [0x00000] in <filename unknown>:0 @ (wrapper remoting-invoke-with-check) system.windows.forms.datagridview:set_datasource (object) @ purchase_requisition.dbquery.prmonitoringgrid (system.windows.forms.datagridview datagridrequests, system.string purchase_number) [0x00000] in <filename unknown>:0 @ purchase_requisition.frmmain.listboxpr_selectedindexchanged (system.object sender, system.eventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.listbox.onselectedindexchanged (system.eventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.listbox.onitemclick (int32 index) [0x00000] in <filename unknown>:0 @ system.windows.forms.listbox.onmouseuplb (system.object sender, system.windows.forms.mouseeventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.control.onmouseup (system.windows.forms.mouseeventargs e) [0x00000] in <filename unknown>:0 @ system.windows.forms.control.wmlbuttonup (system.windows.forms.message& m) [0x00000] in <filename unknown>:0 @ system.windows.forms.control.wndproc (system.windows.forms.message& m) [0x00000] in <filename unknown>:0 @ system.windows.forms.listbox.wndproc (system.windows.forms.message& m) [0x00000] in <filename unknown>:0 @ system.windows.forms.control+controlwindowtarget.onmessage (system.windows.forms.message& m) [0x00000] in <filename unknown>:0 @ system.windows.forms.control+controlnativewindow.wndproc (system.windows.forms.message& m) [0x00000] in <filename unknown>:0 @ system.windows.forms.nativewindow.wndproc (intptr hwnd, msg msg, intptr wparam, intptr lparam) [0x00000] in <filename unknown>:0
i've found out function flistitems() not @ fault @ all. datagridrequests_selectionchanged event's code triggers exception. result of poor code implementation(my fault). transferred offending code under listboxpr_selectedindexchanged event, because need data datagrid after populated function flistitems().
private void listboxpr_selectedindexchanged(object sender, eventargs e) { cursor = cursors.waitcursor; string prnumberselect; if (varname == 180) { //somecode here condition varname=180 prnumberselect = listboxpr.text.tostring(); class1.flistitems(datagridrequests, prnumberselect); //transferred code datagridrequests_selectionchanged event if (datagridrequests.rowcount > 0 ) { intvar1 = convert.toint32(datagridrequests.currentrow.cells["column"].value); } } else { prnumberselect = listboxpr.text.tostring(); class1.flistitems(datagridrequests, prnumberselect); if (datagridrequests.rowcount > 0 ) { intvar1 = convert.toint32(datagridrequests.currentrow.cells["column"].value); } } cursor = cursors.default; }
thank guys helping me....
Comments
Post a Comment