c# - Sql Exception was caught -
i have method
public static dataset getalldatabasenames() { //instance of connection created sqlconnection sconnection = new sqlconnection(configurationsettings.appsettings["connectionstring"]); //to open connection. sconnection.open(); string selectdatabase = @"select [name] [master..sysdatabases]"; //instance of command created. sqlcommand scommand = new sqlcommand(selectdatabase, sconnection); try { //create dataset. dataset dslistofdatabases = new dataset("master..sysdatabases"); //create dataadapter object. sqldataadapter da = new sqldataadapter(selectdatabase, sconnection); //provides master mapping between sourcr table , system.data.datatable da.tablemappings.add("table", "master..sysdatabases"); //fill dataadapter. da.fill(dslistofdatabases); //bind result combobox non primary key table names dataviewmanager dsv = dslistofdatabases.defaultviewmanager; return dslistofdatabases; } catch(exception ex) { //handles exception , log eventlog original message. eventlog log = new eventlog("application"); log.source = "mfdbanalyser"; log.writeentry(ex.message); return null; } { //checks whether connection still open. if(sconnection.state != connectionstate.closed) { sconnection.close(); } } }
but when call this
public void binddbdropdown() { dataset dstableswithoutforeignkeys = default(dataset); try { //the function getallforeignkeytables() called class pluginmanager. dstableswithoutforeignkeys = dataaccessmaster.getalldatabasenames(); dgresultview.datasource = dstableswithoutforeignkeys.tables["master..sysdatabases"]; } catch(exception ex) { //all exceptions handled , written in eventlog. eventlog logexception = new eventlog("application"); logexception.source = "mfdbanalyser"; logexception.writeentry(ex.message); } }
it getting sql exception caught in catch block
can guys please have @ that.
try:
select [name] [master]..[sysdatabases] // **not** [master..sysdatabases]
(note square brackets)
also; far preferable use using
sqlconnection
, sqlcommand
, etc - implements idisposable
- ensure don't haemorrhage connections.
i.e.
using(var sconnection = new sqlconnection(configurationsettings.appsettings["connectionstring"])) { //to open connection. sconnection.open(); string selectdatabase = @" select [name] [master]..[sysdatabases]"; //instance of command created. using(var scommand = new sqlcommand(selectdatabase, sconnection)) { // etc } }
Comments
Post a Comment