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
975 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

25 Experts available now in Live!

Get 1:1 Help Now