从网格视图中删除行时出错 [英] I am getting error when deleting rows from grid view

查看:106
本文介绍了从网格视图中删除行时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从网格中删除行时使用此代码无法绑定多部分标识符System.Web.UI.WebControls.Label。网格视图





I am using this code when deleting rows from grid The multi-part identifier "System. Web.UI.WebControls.Label" could not be bound. grid view


Label name = (Label)grddata.Rows[0].FindControl("lblname");
//Label salary = (Label)grddata.Rows[0].FindControl("lblsal");
Label id = (Label)grddata.Rows[0].FindControl("lblid.text");
//string id = grddata.DataKeys
//   [e.Item.ItemIndex].ToString();

//SqlDataAdapter da = new SqlDataAdapter("UPDATE empdata SET name ='" + name + "' and salary=" + salary + " ", con);

SqlCommand cmd;
       
cmd = new SqlCommand("delete from empdata name='"+name+"'  ", con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();

showdata();
con.Close();







[edit]添加了代码块[/ edit]




[edit]Code block added[/edit]

推荐答案

在行删除事件下尝试此操作:



Try this under the row deleting event:

 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
Label name= (Label)GridView1.Rows[e.RowIndex].FindControl("lblname");
            SqlConnection CN = new SqlConnection(Str_Conn);
            CN.Open();
            SqlCommand myCommand = new SqlCommand("Delete from empData Where name=@name", CN);

            myCommand.Parameters.Add(new SqlParameter("name", SqlDbType.VarChar)).Value = name.Text ;
            SqlDataReader myReader = default(SqlDataReader);
            myReader = myCommand.ExecuteReader();
lblStatus.text="Data deleted successfully."
}


这是因为以下一行。

That is because of the following line.
Label id = (Label)grddata.Rows[0].FindControl("lblid.text");



它应该是。


It should be.

Label id = (Label)grddata.Rows[0].FindControl("lblid");



这个id从未在该函数中使用过。所以,也不需要阅读它。


Bit this "id" is never used in that function. So, no need to read it either.

Label id = (Label)grddata.Rows[0].FindControl("lblid.text");



还有一件事,你总是从 Row [0] 中读取名字,这不是正确的方法。

您应该使用 zahid 建议的 e.RowIndex 删除行中的名称。


One more thing, you are always reading the name from Row[0], which is not the correct way.
You should read the name from the Row, which is being deleted using e.RowIndex, already suggested by zahid.


您没有在查询中写 Where 关键字,因此显示错误

尝试以这种方式更正





protected void GridView1_RowDeleting(object sender,GridViewDeleteEventArgs e)

{

Label name =(标签)GridView1.Rows [e.RowIndex] .FindControl(lablename);

SqlConnection cn = new SqlConnection(Str_Conn);

cn.Open();

SqlCommand c md = new SqlCommand(从empData中删除其中 name ='+ name.Text +',cn);

cmd.ExecuteNonQuery();

cmd.dispose();

cn.Close();

lablemsg.Text =数据删除..;

}
You are not write the Where Keyword in your query so its show the error
try to correct like this way


protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label name= (Label)GridView1.Rows[e.RowIndex].FindControl("lablename");
SqlConnection cn = new SqlConnection(Str_Conn);
cn.Open();
SqlCommand cmd = new SqlCommand("Delete from empData Where name='"+name.Text+"'", cn);
cmd.ExecuteNonQuery();
cmd.dispose();
cn.Close();
lablemsg.Text="Data deleted..";
}


这篇关于从网格视图中删除行时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆