datagrid批量删除出错!
datagrid内定义了一个模版列,用于全选,datagrid外定义了一个删除按钮!删除操作时没有进行任何操作!部分代码如下:
可能是在遍历datagrid时,在 checkbox被选中时,获取该行的Cells[0]中的值,作为删除语句中user_ID的值时 出错,请大家改正下,谢谢!
private void Del_Button_Click(object sender, System.Web.UI.WebControls. CommandEventArgs e)
{
bool noChecked = true;
foreach(DataGridItem dgi in DataGrid1.Items)
{
CheckBox cb=(CheckBox)dgi.FindControl( "chkSelect ");
if(cb.Checked)
{//如果cb被选中的话,执行删除操作
noChecked = false;
// string user=(string)dgi.Cells[0].Text.ToString();
// string strSql = "DELETE FROM user_infor4 WHERE user_ID = user ";
string strSql = "DELETE FROM user_infor4 WHERE user_ID=@user_ID ";
ExecuteSql(strSql);
}
}
if(noChecked == true)
{
Response.Write( " <script> alert( '您没有选择要删除的项目! ') </script> ");
}
user_DGrid4();
}
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings. AppSettings[ "DSN "];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(
System.Exception ex)
{
Page.RegisterStartupScript( "error ",ex.Message);
}
}
------解决方案--------------------string strSql = "DELETE FROM user_infor4 WHERE user_ID= ' "+ user+ " ' ";
如果user取值没有错的话.
------解决方案--------------------string strSql = "DELETE FROM user_infor4 WHERE user_ID= ' "+ user+ " ' ";
建议用单步调试来观察strSql 和user来判断你程序的正确性!