c# - Object reference not set to an instance of object -
i have function this
public void gettableswithuppercasename() { sqlconnectionstringbuilder objconnectionstring = new sqlconnectionstringbuilder(); objconnectionstring.datasource = txthost.text; objconnectionstring.userid = txtusername.text; objconnectionstring.password = txtpassword.text; objconnectionstring.initialcatalog = convert.tostring(cmbdatabases.selectedvalue); sqlconnection sconnection = new sqlconnection(objconnectionstring.connectionstring); //to open connection. sconnection.open(); //query select table_names have names in uppercase letters. string selecttableswithuppercasename = @"select name sysobjects upper(name) collate latin1_general_bin = name collate latin1_general_bin , objectproperty(id,n'istable')=1 , objectproperty(id,n'ismsshipped')=0 "; //create command object sqlcommand scommand = new sqlcommand(selecttableswithuppercasename, sconnection); try { //create dataset dataset dslistoftableswithuppercasename = new dataset("sysobjects"); //create dataadapter object sqldataadapter sdataadapter = new sqldataadapter(selecttableswithuppercasename, sconnection); //provides master mapping between sourcr table , system.data.datatable sdataadapter.tablemappings.add("table", "sysobjects"); //fill dataset sdataadapter.fill(dslistoftableswithuppercasename); //bind result combobox foreign key table names dataviewmanager dvmlistofforeignkeys = dslistoftableswithuppercasename.defaultviewmanager; dgresultview.datasource = dslistoftableswithuppercasename.tables["sysobjects"]; } catch(exception ex) { //all exceptions handled , written in eventlog. eventlog log = new eventlog("application"); log.source = "mfdbanalyser"; log.writeentry(ex.message); } { //if connection not closed close connection if(sconnection.state != connectionstate.closed) { sconnection.close(); } } }
and function counting rows generated previous functions. function
null reference exception or object reference not set instance of object..
can me in this... why catching error functions above , working fine other similar functions.
private void updatelabeltext() { sqlconnectionstringbuilder objconnectionstring = new sqlconnectionstringbuilder(); objconnectionstring.datasource = txthost.text; objconnectionstring.userid = txtusername.text; objconnectionstring.password = txtpassword.text; objconnectionstring.initialcatalog = convert.tostring(cmbdatabases.selectedvalue); sqlconnection sconnection = new sqlconnection(objconnectionstring.connectionstring); //to open connection. sconnection.open(); try { int selectedcelltotal = 0; int counter; // iterate through selectedcells collection , sum values. for(counter = 0;counter < (dgresultview.selectedcells.count);counter++) { if(dgresultview.selectedcells[counter].formattedvaluetype == type.gettype("system.string")) { string value = null; // if cell contains value has not been commited, if(dgresultview.iscurrentcelldirty == true) { value = dgresultview.selectedcells[counter].editedformattedvalue.tostring(); } else { value = dgresultview.selectedcells[counter].formattedvalue.tostring(); } if(value != null) { // ignore cells in description column. if(dgresultview.selectedcells[counter].columnindex != dgresultview.columns["table_name"].index) { if(value.length != 0) { selectedcelltotal += int.parse(value); } } } } } // set labels reflect current state of datagridview. lbldisplay.text = "there total " + dgresultview.rowcount + cmboperations.selecteditem.tostring(); } catch(exception ex) { //all exceptions handled , written in eventlog. eventlog log = new eventlog("application"); log.source = "mfdbanalyser"; log.writeentry(ex.message); } { //if connection not closed close connection if(sconnection.state != connectionstate.closed) { sconnection.close(); } } }
also lbldisplay.text not taking proper spaces.
waiting reply
ok, don't have answer why you're getting "null reference exception" - few points throw in, nonetheless:
i use
sys.tables
instead ofsysobjects
, having specify type of object query foralways put disposable
sqlconnection
,sqlcommand
using(.....) { ...... }
blocks. way, won't needfinally {..}
blocks, , .net take care of disposing of objects when they're no longer neededwhy use
dataset
when have single table inside?? that's unnecessary overhead - usedatatable
instead!don't open
sqlconnection
- wait 'til last moment, open it, execute query, close again right awayactually, when using
sqldataadapter
, don't need opensqlconnection
@ -sqldataadapter
(and close again after done reading data)do not mix retrieval of data database binding ui element - bad practice.
gettableswithuppercasename
method, should return (likedatatable
) caller (the ui) , let ui handle binding processalong same lines: method should not grabbing stuff ui elements (like text boxes) - pass in values method parameters, cleaner code - 1 might able reuse in project day
this how think first method ought like
public datatable gettableswithuppercasename(string server, string database, string username, string password) { // create datatable datatable dtlistoftableswithuppercasename = new datatable("tablenames"); sqlconnectionstringbuilder objconnectionstring = new sqlconnectionstringbuilder(); objconnectionstring.datasource = server;; objconnectionstring.userid = username; objconnectionstring.password = password; objconnectionstring.initialcatalog = database; // define query against sys.tables - easier , cleaner! string selecttableswithuppercasename = "select name sys.tables upper(name) collate latin1_general_bin = name collate latin1_general_bin , is_msshipped = 0"; // put sqlconnection , sqlcommand using blocks! using (sqlconnection sconnection = new sqlconnection(objconnectionstring.connectionstring)) using (sqlcommand scommand = new sqlcommand(selecttableswithuppercasename, sconnection)) { try { // create dataadapter object sqldataadapter sdataadapter = new sqldataadapter(selecttableswithuppercasename, sconnection); // fill datatable - no need open connection, sqldataadapter // (and close again after done) sdataadapter.fill(dtlistoftableswithuppercasename); } catch (exception ex) { //all exceptions handled , written in eventlog. eventlog log = new eventlog("application"); log.source = "mfdbanalyser"; log.writeentry(ex.message); } } // return data table caller return dtlistoftableswithuppercasename; }
Comments
Post a Comment