Click here to Skip to main content
16,005,149 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


C#
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]Code block added[/edit]
Posted
Updated 15-Feb-14 9:27am
v3
Comments
Krunal Rohit 15-Feb-14 23:10pm    
can you post the .aspx code ?

Try this under the row deleting event:

C#
 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."
}
 
Share this answer
 
v2
That is because of the following line.
C#
Label id = (Label)grddata.Rows[0].FindControl("lblid.text");

It should be.
C#
Label id = (Label)grddata.Rows[0].FindControl("lblid");

Bit this "id" is never used in that function. So, no need to read it either.
C#
Label id = (Label)grddata.Rows[0].FindControl("lblid.text");

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.
 
Share this answer
 
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..";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900