2

I just want to know if I did the right way in using the "using" in my CRUDStudentTable().

private string Query;
public string message;

public CRUD(string myQuery)
{
   Query = myQuery;
}

public void CRUDStudentTable()
    {
            using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Microsoft User\Desktop\Student.accdb"))
            {
                try
                {
                    conn.Open();
                    using (OleDbCommand cmd = new OleDbCommand(Query, conn))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception exception)
                {
                    message = exception.Message;
                }
                finally
                {
                    conn.Close();
                }
            }
    }

public string HasError()
    {
        if (string.IsNullOrEmpty(message))
        {
            message = "Successful";
            return message;
        }
        else
        {
            return message;
        }
    }

If this is not right, kindly give me an advice on how to do this right. Thank you.

I included here another method that return the "message" whenever it is not null.

2
  • 3
    Get rid of the entire try block. Commented Nov 24, 2013 at 3:10
  • I used the try block to catch errors. Commented Nov 24, 2013 at 3:11

1 Answer 1

5

This is a kind of "belt and suspenders" approach: you should use either a using or a finally, but not both.

The using block guarantees to close your connection in all code paths, including the ones with exceptions. Therefore you can rewrite the code as follows:

public void CRUDStudentTable()
{
        using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\Microsoft User\Desktop\Student.accdb"))
        {
            try
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(Query, conn))
                {
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception exception)
            {
                message = exception.Message;
                // Consider re-throwing the exception here
                // to let callers know what happened.
                // Silently harvesting the message and continuing
                // is not a good practice of handling exceptions.
            }
        }
}
Sign up to request clarification or add additional context in comments.

7 Comments

Probably it would also worth mentioning that catch (Exception exception) { message = exception.Message; } would be considered as a bad practice
@zerkms not necessarily, it all depends on what happens with message. I have done similar in WCF situations where allowing a exception to bubble up would fault the WCF channel when I have other ways of reporting the error without faulting the channel.
@Scott Chamberlain: I didn't propose it to bubble up to the very top and get an unhandled exception, but it shouldn't be silently writing an exception message into an object's property without return false; or something like that to indicate it failed
@user3026391: and how would you know if an error even occurred? Your method returns void, how would you know the result of the query execution?
@zerkms I edited my post. It shows there what I did with the "message". If I also did it the wrong way, please correct me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.