mysql - Getting Connection Error Connection Must Be Valid and Open -
this data access layer don't know why getting connection error when multi user using , when more 1 user access website checked thing cant fix posting code. updated code. again stuck , result same last code. dnt know why happened when more 1 user try access login not working more after 1 2 mints
using system; using system.collections.generic; using system.linq; using system.web; using system.data; using mysql.data; using mysql.data.mysqlclient; using system.configuration; public class clsmysqlconnection { public mysqlconnection con = null; protected mysqlconnection getconnection() { if (con == null || con.state != connectionstate.open) { con = new mysqlconnection(configurationmanager.connectionstrings["conn"].connectionstring); con.open(); } return (con); } } public class clsmanagement : clsmysqlconnection { public datatable getdata(string mysql) { mysqlcommand cmd = new mysqlcommand(); mysqldataadapter da = new mysqldataadapter(); datatable dt = new datatable(); cmd.commandtext = mysql; cmd.connection = getconnection(); da.selectcommand = cmd; da.fill(dt); return (dt); } public mysqldatareader executeactionquery(string mysql) { mysqlcommand cmd = new mysqlcommand(); cmd.commandtext = mysql; try { cmd.connection = getconnection(); cmd.executenonquery(); mysqldatareader rdr = cmd.executereader(); return (rdr); } catch (exception) { throw; } { if (con.state == connectionstate.open) con.close(); } } public int executedml(string mysql) { mysqlcommand cmd = new mysqlcommand(); cmd.commandtext = mysql; try { cmd.connection = getconnection(); return cmd.executenonquery(); } catch (exception) { throw; } { if (con.state == connectionstate.open) con.close(); } } }
you can create new connection , return getconnection()
function.
protected mysqlconnection getconnection() { mysqlconnection con = new mysqlconnection(configurationmanager.connectionstrings["conn"].connectionstring); con.open(); }
and use con
variable in local scope.
Comments
Post a Comment