Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 13th, 2006, 10:10 AM
Authorized User
 
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default coding error using checkbox in datagrid

i don't know if this write place to post or not....
below is the code to delete using check box in datagrid the same is working fine in vb.net but not in c # can anybody help me....it gives two error which r described at the end...
Code:
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace CVTracker
{
    /// <summary>
    /// Summary description for test.
    /// </summary>
    public class test : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button btnShow;
        protected System.Web.UI.WebControls.DataGrid dtgCusts;
        protected System.Web.UI.WebControls.Label lblSelections;

        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!(IsPostBack)) 
            {
                btnShow.Attributes.Add("onClick", "javascript:return confirm('Are you sure you want to delete these rows?')");
                BindTheData();
            }
        }

        void BindTheData()
        {
            SqlConnection objConn;
            SqlCommand objCmd;
            objConn = new SqlConnection("server=(local);database=Northwind;uid=sa;pwd=;");
            string strSql;
            strSql = "SELECT Top 10 CustomerID, CompanyName, ContactName, " + "ContactTitle, City, Country, Phone FROM Customers";
            try 
            {
                objCmd = new SqlCommand(strSql, objConn);
                objConn.Open();
                dtgCusts.DataSource = objCmd.ExecuteReader();
                dtgCusts.DataBind();
            } 
            catch 
            {
            } 
            finally 
            {
                if (objConn.State == ConnectionState.Open) 
                {
                    objConn.Close();
                    objConn.Dispose();
                }
            } 
        }


        void ShowSelections(object sender, System.EventArgs e)
        {
            DataGridItem dgItem;
            CheckBox chkSelected;
            string strCompanyName;
            string strCustomerID;
            lblSelections.Text = "<br>Fooled Ya! The following rows were marked for deletion, ";
            lblSelections.Text += "but not actually deleted:<br><br>";
            foreach (int dgItem in dtgCusts.Items) 
            {
                chkSelected = dgItem.FindControl("chkSelection");
                if (chkSelected.Checked) 
                {
                    strCompanyName = ((Label)(dgItem.FindControl("lblCompanyName"))).Text;
                    strCustomerID = ((Label)(dgItem.FindControl("lblCustomerId"))).Text;
                    lblSelections.Text += "Company Name: <b>" + strCompanyName + "</b> | ";
                    lblSelections.Text += "Customer ID: <b>" + strCustomerID + "</b><br>";
                }
            }
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {    
            this.btnShow.Click += new System.EventHandler(this.btnShow_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void btnShow_Click(object sender, System.EventArgs e)
        {

        }
    }
}
error{1} c:\inetpub\wwwroot\CVTracker\test.aspx.cs(71): A local variable named 'dgItem' cannot be declared in this scope because it would give a different meaning to 'dgItem', which is already used in a 'parent or current' scope to denote something else

error {2} c:\inetpub\wwwroot\CVTracker\test.aspx.cs(73): Cannot implicitly convert type 'System.Web.UI.Control' to 'System.Web.UI.WebControls.CheckBox'


Best Regards,
Vivek.
__________________
Best Regards,
Vivek.
 
Old March 13th, 2006, 04:16 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

dgitem is not an integer type it is a datagrid item type.



 
Old March 14th, 2006, 01:09 AM
Authorized User
 
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

my first suggestion will be try removing the 'int' in for loop and add '(Check box) ' in front of dgItem.FindControl("chkSelection");
lemme write it, replace the quoted part with the one that follows it.

Quote:
quote:

            foreach (<s>int</s> dgItem in dtgCusts.Items)
            {
                chkSelected = dgItem.FindControl("chkSelection");
Quote:
quote:
            foreach (dgItem in dtgCusts.Items)
            {
                chkSelected = (CheckBox) dgItem.FindControl("chkSelection");

we'll try something else if this doesn't work.

Regards,
Sai Puli
 
Old March 14th, 2006, 01:55 AM
Authorized User
 
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Default

dear Sai Puli,

i had tried this before it give an warning message
Type and identifier both are requried in for each statement
Quote:
quote:
            foreach (dgItem in dtgCusts.Items)
            {
                chkSelected = (CheckBox) dgItem.FindControl("chkSelection");
we need to try something else or any other code that gives the selected checkbox value in datagrid....


[/quote]

Best Regards,
Vivek.
 
Old April 28th, 2008, 03:36 PM
Registered User
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Use:

foreach (DataGridItem dgItem in dtgCusts.Items)


instead of

foreach (dgItem in dtgCusts.Items)

This is because the foreach statemnt needs dgItem to be declared before it is used!


Thanks :)





Similar Threads
Thread Thread Starter Forum Replies Last Post
checkbox in datagrid asad_prog VB.NET 0 November 25th, 2006 02:41 PM
CheckBox in DataGrid Baby_programmer ASP.NET 1.x and 2.0 Application Design 2 March 11th, 2005 12:42 AM
CheckBox in DataGrid Baby_programmer ASP.NET 1.0 and 1.1 Basics 1 March 3rd, 2005 08:33 PM
checkbox in datagrid sundar_revathi General .NET 1 June 3rd, 2004 02:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.