 |
| ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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
|
|
|
|

March 13th, 2006, 10:44 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
checkbox in datagrid for deleteing ..
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.
__________________
Best Regards,
Vivek.
|
|

March 13th, 2006, 11:55 AM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:
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)
|
Dear vivek,
I am more in to vb.net than C#, but based on common analysis I highlited couple of lines in your code where you declared dgItem as DataGridItem and later in a for loop of the same function you are declaring the dgItem as an int. This might be a problem and also about the implicit conversion error try type casting ( like CType in vb.net).
Regards,
Sai Puli
|
|

March 13th, 2006, 12:24 PM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dear Sai Puli,
i have converted this code from vb.net to c # with help of the online converter there..
i m getting error on tose two lines that u have hi-lighted... can u help me getting this error solved..
Thanks.
Best Regards,
Vivek.
|
|

March 13th, 2006, 12:46 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 (int dgItem in dtgCusts.Items)
{
chkSelected = dgItem.FindControl("chkSelection");
|
Quote:
quote:
foreach (dgItem in dtgCusts.Items)
{
chkSelected = (CheckBox) dgItem.FindControl("chkSelection");
|
lets try something else if this doesn't work.
Regards,
Sai Puli
|
|

March 14th, 2006, 02:01 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dear Sai Puli,
i had tried this before but it gives warning message
Type and identifier are both requried in the for each statement
Quote:
quote:
foreach (dgItem in dtgCusts.Items)
{
chkSelected = (CheckBox) dgItem.FindControl("chkSelection");
|
we need to try something else or some other code that give the selected checkbox value from in the datagrid...
can u help me in doing so..
|
|

March 14th, 2006, 12:56 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
then try removing the declaration
DataGridItem dgItem;
and changing the foreach as
foreach(DataGridItem dgitem in dgCusts.Items)
regards,
Sai Puli
|
|

March 14th, 2006, 01:02 PM
|
|
Authorized User
|
|
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is a nice article from codeproject.com whcih I guess will really help you solve your problem
http://www.codeproject.com/aspnet/ChkBoxInGrid.asp
Regards,
Sai Puli.
|
|

March 16th, 2006, 01:41 AM
|
|
Authorized User
|
|
Join Date: Nov 2005
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
dear Sai Puli,
thanks for ur help it worked i tried the making test application in vb.net and than same back in c# i it is working fine....
thanks and all the best,
vivek soni.
Best Regards,
Vivek.
|
|
 |