creating a common oracle database connection using a single form c# for several forms -


i have created oracle database connection in 1 form(form1) , used same connection in form(form2) retrieve data database!.but error unhandled exception when trying retrieve data onto label in second form!

the code of form1 follows(it working properly!)

 public void connection()         {             con = new oracleconnection("data source = (description = (address_list = (address = (protocol = tcp)(host ="+ip.text+")(port ="+port.text+"))) " +             " (connect_data = (server = shared) (service_name = ora12c) )" +         " ); user id =" + uname.text + "; password=" + pword.text);          }          private void button1_click(object sender, eventargs e)         {             connection();             con.open();                  form2 f2 = new form2();                 f2.show();           } 

the code in second form2

private void button1_click(object sender, eventargs e)         {             form1 f1 = new form1();             f1.connection();             f1.con.open();             oraclecommand cmd = new oraclecommand();             cmd.commandtext = "select name username";             cmd.connection = f1.con;             oracledatareader dr = cmd.executereader();             dr.read();             label1.text = dr.getstring(0);         } 

when enter details(username,password,etc) , click lgin button in form1 access form2 in form2 when click load button geth error "an unhandled exception of type 'oracle.dataaccess.client.oracleexception' occurred in oracle.dataaccess.dll" highlighting f1.con.open();

you creating new instance of form1 in button1_click1 ofform2. due connection string inf1.connection1is generated using blank values of text boxes ofform1`.

to resolve issue need make sure connection created in form1.connection method should stored somewhere can use same connection in form2 also.

consider having helper class maintains connection object static object inside it. can set @ 1 place , retrieve @ other place.

public static class databasehelper {     private static oracleconnection oracleconnection;      private static string connectionstringtemplate = "data source = (description = (address_list = (address = (protocol = tcp)(host ={0})(port ={1}))) (connect_data = (server = shared) (service_name = ora12c) ) ); user id ={2}; password={3}";      public static void setconnection(string ipaddress, string portnumber, string username, string password)     {         var connectionstring = string.format(connectionstringtemplate, ipaddress, portnumber, username, password);          oracleconnection = new oracleconnection(connectionstring);     }      public static void setconnection(string connectionstring)     {         oracleconnection = new oracleconnection(connectionstring);     }      public static oracleconnection getconnection()     {         return oracleconnection;     } } 

now can use call databasehelper.setconnection method form1 set connection.

private void button1_click(object sender, eventargs e) {     databasehelper.setconnection(ip.text,port.text, uname.text, pword.text);     form2 f2 = new form2();     f2.show(); } 

and retrieve , reuse connection in form2 following.

private void button1_click(object sender, eventargs e) {     oracleconnection con = databasehelper.getconnection();      oraclecommand cmd = new oraclecommand();     cmd.commandtext = "select name username";     cmd.connection = con;     con.open();     oracledatareader dr = cmd.executereader();     dr.read();     label1.text = dr.getstring(0); } 

this should resolve issue.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -