return a value from Try - Catch block?

Posted on 2007-07-23
Medium Priority
Last Modified: 2012-06-21
Hello experts,

I am trying to secure my code by using try catch blocks where necessary. Here is a method I use:

 public int doesExist()
                conn.ConnectionString = strConn;

                cmdCheck.Connection = conn;
                cmdCheck.CommandText = ConfigurationSettings.AppSettings["spCheck"];
                cmdCheck.CommandType = CommandType.StoredProcedure;
                int iq = (int)cmdCheck.ExecuteScalar();    

               return iq;

Whenever I want to put this piece between  a try block, I get this error:

Error      5      The name 'iq' does not exist in the current context

How do I secure my code now?

Question by:bozer
LVL 35

Accepted Solution

mrichmon earned 200 total points
ID: 19551630
You don't put try-catches around function declarations. (and also try/catch is not a way to secure code...)

You can put around the call

int result = -1;

   result = doesExist();
catch(whatever exception you are catching)
   // do stuff

//use result...

Author Comment

ID: 19552109

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question