return a value from Try - Catch block?

Posted on 2007-07-23
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

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now