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.