GridView1.SelectedIndex = -1 because no index is selected in the gridview
try the below code...
private void BindGrid()
{
int selectedRowIndex = GridView1.SelectedIndex;
if(selectedRowIndex>0)
{
int pId = (int)GridView1.DataKeys[selectedRowIndex].Value;
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString =
ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ProductId, ProductName, Price FROM Product " +
"WHERE ProductId=@ProductId", conn);
comm.Parameters.Add("ProductId", SqlDbType.Int);
comm.Parameters["ProductId"].Value = pId;
try
{
conn.Open();
reader = comm.ExecuteReader();
employeeDetails.DataSource = reader;
employeeDetails.DataKeyNames = new string[] { "ProductId" };
employeeDetails.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
}