c# - BeginTransaction() on sqltransaction freezes, how to use try catch to solve it? -


i'm using sqltransaction transaction = connection.begintransaction() in c# application data sqlserver.

the problem have when there no connection server (no internet connection), application freezes , stops working, no exception, no error, freezes.

i tried use try catch catch error, application freezes, , option kill application. can me catch error , istead of freezeing give me error - connection server failed. please check internet connection.

here class code connection:

public static class requestid {     // methods     public static int getid(string server, string database, string user, string pass)     {         int num = 0;         using (sqlconnection connection = new sqlconnection(string.format("server={0};database={1};uid={2};pwd={3};connect timeout=900", new object[] { server, database, user, pass })))         {             sqlcommand command = new sqlcommand("select value_int param code= 'counter'");             sqlcommand command2 = new sqlcommand("update param set value_int = value_int + 1 code= 'counter'");             if (connection.state != connectionstate.open)             {                 connection.open();             }             try             {                 using (sqltransaction transaction = connection.begintransaction())                     {                         try                         {                              command.connection = connection;                             command.transaction = transaction;                             command2.connection = connection;                             command2.transaction = transaction;                             num = (int)command.executescalar();                             command2.executenonquery();                             transaction.commit();                         }                         catch (exception)                         {                             transaction.rollback();                             throw;                         }                                                 {                             if (connection.state != connectionstate.closed)                             {                                 connection.close();                             }                         }                     }                 }              catch (exception ex)             {                 throw;             }                           return num;         }     } } 

problem in line:

using (sqltransaction transaction = connection.begintransaction()) 

thanks in advance.

p.s. application works fine, problam far connection internet lost. wasn't able find solution...

your problem because

connection.open(); 

is outside of try block never hit catch. move inside try block.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -