Question

I'm using SqlTransaction transaction = connection.BeginTransaction() in my C# application to get data from SQLServer.

The problem I have when there is no connection to server (no internet connection), then my application just freezes and stops working, no exception, no error, just freezes.

I tried use try catch to catch error, but even then application just freezes, and the only option is to kill application. Can anyone help me to catch this error and istead of freezeing give me error - like Connection to server failed. Please check internet connection.

Here is my class code for 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 FROM Param WHERE code= 'counter'");
            SqlCommand command2 = new SqlCommand("UPDATE Param SET Value_Int = Value_Int + 1 WHERE 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;
                        }
                        finally
                        {
                            if (connection.State != ConnectionState.Closed)
                            {
                                connection.Close();
                            }
                        }
                    }
                }

            catch (Exception ex)
            {
                throw;
            }            


            return num;
        }
    }
}

Problem is in line:

using (SqlTransaction transaction = connection.BeginTransaction())

Thanks in advance.

P.S. Application works just fine, the only problam so far then connection to internet lost. I wasn't able to find solution...

Was it helpful?

Solution

Your problem is because

connection.Open();

is outside of your try block so you will never hit the catch. Move that inside your try block.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top