Solved

Try Catch (Exception e) 'e' cannot be decalred in this scope because it would give a different meaning to 'e'

Posted on 2011-03-23
7
548 Views
Last Modified: 2013-12-17
I am trying to delete a record using LINQ and then submit the changes in a Try/Catch statement but it will not let me trap for errors.  Can someone tell me what I am missing.   Here is my code:


DataClasses1DataContext dcDataClass1 = new DataClasses1DataContext();


            PatientMed myPatientMed = new PatientMed();

            MyGlobalVars.DeleteRecordKey = this.UltraWebGridSelectedMeds.DisplayLayout.SelectedRows[0].Cells.FromKey("PatMedsKey").Text;

            var SelectedMedsResult = from selectedmedsdata in dcDataClass1.PatientMeds
                                     where selectedmedsdata.PatMedsKey.ToString() == MyGlobalVars.DeleteRecordKey
                                     select selectedmedsdata;


            foreach (var selectedmedsresultrecord in SelectedMedsResult)
            {
                

                dcDataClass1.PatientMeds.DeleteOnSubmit(selectedmedsresultrecord);

                try
                {
                    dcDataClass1.SubmitChanges();

                }
                catch (Exception e)  <<<  ------------ error occurs here
                {
                    // --
                }

Open in new window

0
Comment
Question by:kwh3856
  • 4
  • 2
7 Comments
 

Author Comment

by:kwh3856
ID: 35203780
Actual error message:

Error 1 A local variable named 'e' cannot be declared in this scope because it would give a different meaning to 'e', which is already used in a 'parent or current' scope to denote something else      
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35203791
Most likely the EVENT that this code is in has a PARAMETER that is called "e".

All you need to do is change the name of your Exception variable in the Catch block to something else.

Change:

                catch (Exception e)  
                {
                    // --
                }

To:

                catch (Exception e2)  
                {
                    // ... use "e2" in here ...
                }
0
 

Author Comment

by:kwh3856
ID: 35203792
I think it may be referring to the following code but still not sure how to correct it.  Tried to change it to (Exception x) but then got the error "The variable 'x' is declared but never used"



protected void UltraWebGridSelectedMeds_SelectedRowsChange(object sender, SelectedRowsEventArgs e)
        {

            DataClasses1DataContext dcDataClass1 = new DataClasses1DataContext();

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35203798
This is simply a WARNING:

    "The variable 'x' is declared but never used"

that means you are not using the 'x' variable anywhere in the Catch{} block.  It should still compile and run fine.
0
 

Author Comment

by:kwh3856
ID: 35203803
ok i will try - I did not notice it was only a warning.
0
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35203805
have you uses x in catch yet?

for ex.

System.Console.WriteLine("Error message : " + x.Message);
0
 

Author Closing Comment

by:kwh3856
ID: 35203853
Thank you.  That was exactly it.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

911 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

21 Experts available now in Live!

Get 1:1 Help Now