Solved

A local variable named 'myDataGridItem' cannot be declared in this scope because it would give a different meaning to 'myDataGridItem',

Posted on 2006-06-29
16
992 Views
Last Modified: 2008-02-20
Hi experts,

I was wondering if anyone can explain why this has happened.

private void btnSelect_ServerClick(object sender, System.EventArgs e)
            {
                  DataGridItem myDataGridItem;
                  System.Web.UI.WebControls.CheckBox chkSelected;
                  string strStoreName;
                  string strStoreID;
                  lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
                  foreach (int myDataGridItem in DG_CheckBox.Items)
                  {
                        chkSelected = myDataGridItem.FindControl ("chkSelection");
                        if (chkSelected.Checked)
                        {
                              strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                              strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                              lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                              lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                        }
                  }
            }


in this line
foreach (int myDataGridItem in DG_CheckBox.Items)

it says
 A local variable named 'myDataGridItem' cannot be declared in this scope because it would give a different meaning to 'myDataGridItem', which is already used in a 'parent or current' scope to denote something else

and

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBox'

thanks Guys
0
Comment
Question by:SirReadAlot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
16 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17008224
i think its here:

chkSelected = myDataGridItem.FindControl ("chkSelection");

the

.FindControl method retruns a Conrol and yyou are trying to populate it into a checkBox object

if you use this it will work:

System.Web.UI.Control MyControl;
...
MyControl = myDataGridItem.FindControl ("chkSelection");

what line is it erroring on
0
 

Author Comment

by:SirReadAlot
ID: 17008230
it was erroring on this

int myDataGridItem in this line

foreach (int myDataGridItem in DG_CheckBox.Items)
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 17008255
DataGridItem myDataGridItem;
...
foreach (int myDataGridItem in DG_CheckBox.Items)

You define myDataGridItem twice. First time as DataGridItem, second time as int. Maybe you need to remove first line and change second line:

foreach (DataGridItem myDataGridItem in DG_CheckBox.Items)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:apresto
ID: 17008256
i see

what is the return type of DG_CheckBox.Items (just however over items and it will tell you)

whatever it is, replace int with that type:

foreach (INT myDataGridItem in DG_CheckBox.Items) <!-- change int here for the type returned by '.Items'
0
 

Author Comment

by:SirReadAlot
ID: 17008257
I have tried this

private void btnSelect_ServerClick(object sender, System.EventArgs e)
            {
                  DataGridItem myDataGridItem;
                  System.Web.UI.WebControls.CheckBox chkSelected;
                  System.Web.UI.Control MyControl;

                  string strStoreName;
                  string strStoreID;
                  lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
                  foreach (int myDataGridItem in DG_CheckBox.Items)
                  {
                        //chkSelected = myDataGridItem.FindControl ("chkSelection");
                        MyControl = myDataGridItem.FindControl ("chkSelection");

                        if (chkSelected.Checked)
                        {
                              strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                              strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                              lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                              lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                        }
                  }
            }
but still errors on the same line
0
 
LVL 23

Expert Comment

by:apresto
ID: 17008272
its still the same:

foreach (int myDataGridItem in DG_CheckBox.Items)
0
 

Author Comment

by:SirReadAlot
ID: 17008273
okay, that remove the error

private void btnSelect_ServerClick(object sender, System.EventArgs e)
            {
                  //DataGridItem myDataGridItem;
                  System.Web.UI.WebControls.CheckBox chkSelected;
                  string strStoreName;
                  string strStoreID;
                  lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
                  foreach (DataGridItem myDataGridItem in DG_CheckBox.Items)
                  //foreach (int myDataGridItem in DG_CheckBox.Items)
                  {
                        chkSelected = myDataGridItem.FindControl ("chkSelection");
                        if (chkSelected.Checked)
                        {
                              strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                              strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                              lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                              lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                        }
                  }
            }
0
 
LVL 23

Expert Comment

by:apresto
ID: 17008282
think about it, with this line you are saying

foreach (int myDataGridItem in DG_CheckBox.Items)

For Each integer in the collection returned by the DG_CheckBox.Items property, do something

But does DG_CheckBox.Items return a collection of integers (int[]) - i dont tihnk so!

Change it as AlexFM has suggested:

foreach (DataGridItem myDataGridItem in DG_CheckBox.Items)
0
 
LVL 23

Expert Comment

by:apresto
ID: 17008286
sorry didnt see the previous post
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17008289
I think you misread AlexFM's post. The problem isn't the case of INT in the loop, its the fact that the variable you are using in your loop is called "myDataGridItem".

private void btnSelect_ServerClick(object sender, System.EventArgs e)
          {
               DataGridItem myDataGridItem;                                                        // <-- Declares a variable called myDataGridItem
               System.Web.UI.WebControls.CheckBox chkSelected;
               string strStoreName;
               string strStoreID;
               lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
               foreach (int myDataGridItem in DG_CheckBox.Items)                          // <-- Declares a second variable, also called myDataGridItem.  You need to rename this to something else
               {
                    chkSelected = myDataGridItem.FindControl ("chkSelection");
                    if (chkSelected.Checked)
                    {
                         strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                         strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                         lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                         lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                    }
               }
          }
0
 

Author Comment

by:SirReadAlot
ID: 17008318
Hi carl,

I have declared it in one like and the error is gone
private void btnSelect_ServerClick(object sender, System.EventArgs e)
            {
      
                  System.Web.UI.WebControls.CheckBox chkSelected;

                  string strStoreName;
                  string strStoreID;
                  lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
                  foreach (DataGridItem myDataGridItem in DG_CheckBox.Items)
                  {
                        chkSelected = myDataGridItem.FindControl ("chkSelection");
                        if (chkSelected.Checked)
                        {
                              strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                              strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                              lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                              lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                        }
                  }
            }
0
 

Author Comment

by:SirReadAlot
ID: 17008337
hi don't mean to be a pain, or should i ask this in another post

you forgot

"and

Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBox'
"

private void btnSelect_ServerClick(object sender, System.EventArgs e)
            {
      
                  System.Web.UI.WebControls.CheckBox chkSelected;

                  string strStoreName;
                  string strStoreID;
                  lblStatus.Text = "<br>You seelcted the Following items:<br><br>";
                  foreach (DataGridItem myDataGridItem in DG_CheckBox.Items)
                  {
                        =====> chkSelected = myDataGridItem.FindControl ("chkSelection");
                        if (chkSelected.Checked)
                        {
                              strStoreName = ((Label) (myDataGridItem.FindControl("lblStoreName"))).Text;
                              strStoreID = ((Label)(myDataGridItem.FindControl("hdnStoreID"))).Text;
                              lblStatus.Text +=  "The store name is <b>" + strStoreName + "</b>";
                              lblStatus.Text +=  " and the StoreID is <b>" + strStoreID  + "<b><br>";
                        }
                  }
            }

      chkSelected = myDataGridItem.FindControl ("chkSelection");
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 17008387
chkSelected = (CheckBox)myDataGridItem.FindControl ("chkSelection");
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17008389
chkSelected = (CheckBox)myDataGridItem.FindControl ("chkSelection");
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17008392
FindControl returns a generic Control object, so you have to cast it to the correct type before you can use the type-specific methods and properties.
0
 

Author Comment

by:SirReadAlot
ID: 17008400
THANKS GUY, IT WORKS

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

696 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