Reputation: 39
I am trying to execute below code and i am getting an error saying :
Name' row' does not exist in current context
Code
protected void wgrdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
int index = Convert.ToInt32(wgrdSearchResult.DataKeys[row.RowIndex].Value);
//int index = Int32.Parse((string)e.CommandArgument);
string CustomerID = (string)wgrdSearchResult.DataKeys[index].Values["CustomerID"];
}
}
Upvotes: 0
Views: 2461
Reputation: 1769
Try this:
protected void wgrdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "edit")
{
int index = Convert.ToInt32(wgrdSearchResult.DataKeys[e.CommandArgument].Value);
string CustomerID = (string)wgrdSearchResult.DataKeys[e.CommandArgument].Values["CustomerID"];
}
}
Can you show the code for the front-end of the GridView as well? My code assumes that you either have a single DataKey (that is, CustomerID), or that you have multiple data keys set up for your GridView, where the first data key is some arbitrary "index" that is apparently meaningless and never used while the second key is a meaningful CustomerID.
Upvotes: 0
Reputation: 54532
row is not declared in your method. Look at this MSDN example showing what you seem to be trying to do. From above link:
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index];
Upvotes: 1
Reputation: 23142
You haven't declared any variable named row
, so your assignment to index
is failing.
Upvotes: 0