Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1003
  • Last Modified:

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

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
SirReadAlot
Asked:
SirReadAlot
  • 6
  • 5
  • 3
  • +1
2 Solutions
 
aprestoCommented:
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
 
SirReadAlotAuthor Commented:
it was erroring on this

int myDataGridItem in this line

foreach (int myDataGridItem in DG_CheckBox.Items)
0
 
AlexFMCommented:
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
Technology Partners: 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!

 
aprestoCommented:
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
 
SirReadAlotAuthor Commented:
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
 
aprestoCommented:
its still the same:

foreach (int myDataGridItem in DG_CheckBox.Items)
0
 
SirReadAlotAuthor Commented:
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
 
aprestoCommented:
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
 
aprestoCommented:
sorry didnt see the previous post
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
SirReadAlotAuthor Commented:
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
 
SirReadAlotAuthor Commented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
chkSelected = (CheckBox)myDataGridItem.FindControl ("chkSelection");
0
 
AlexFMCommented:
chkSelected = (CheckBox)myDataGridItem.FindControl ("chkSelection");
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
SirReadAlotAuthor Commented:
THANKS GUY, IT WORKS

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 6
  • 5
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now