Sam1604
Sam1604

Reputation: 1489

How to get gridview row value while rowdeleting?

I want to delete the row of gridview. So i added Commandfield Delete button for all rows. When i click to delete button, it triggers gvBooklist_RowDeleting event. In that i'm giving the following code to get current book name.

string tempBook = gvBooklist.Rows[e.RowIndex].Cells[0].ToString();
SqlCommand cmd = new SqlCommand("Select id from tBooks where bookName = '" + tempBook + "';");

But tempBook returns System.Web.UI.WebControls.DataControlFieldCell. How do i get the actual book name?

Upvotes: 0

Views: 4793

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460360

Instead of ToString use it's Text property:

string tempBook = gvBooklist.Rows[e.RowIndex].Cells[0].Text;

Side note: you also should use sql-parameters - always:

using(var cmd = new SqlCommand("Select id from tBooks where bookName = @bookname"))
{
    cmd.Parameters.Add("@bookname", SqlDbType.VarChar).Value = tempBook;
    // ...
}

Upvotes: 5

Related Questions