Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
1,000 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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