Hello Experts,
I have a page with the GridView control on it. When I select the record inside the grid, the control goes to the "details" page and show the result correctly. But, if I click on the "Back" button in the browser and select another record from the grid, I am getting an error "Object reference not set to an instance of an object"
at the selected line of code shown below.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
XmlDocument usersList = null;
long usersNumber = 0;
int index;
try
{
Session["selectedName"] = GridView1.SelectedDataKey.Values["Name"].ToString();
if (Session["selectedName"].ToString() != "")
{
try
{
//Create the proxy.
ADUsersService proxy = new ADUsersService();
//Call the web service and get the results:
XmlNode myXMLNodeObject = proxy.getUserAccount(Session["selectedName"].ToString());
usersList = new XmlDocument();
usersList.AppendChild(usersList.ImportNode(myXMLNo deObject, true));
if (usersList != null)
{
Session["usersList"] = usersList;
usersNumber = proxy.SearchResultCount();
if (usersNumber != 0)
{
if (usersNumber != 1)
{
//Redirect to the Multy Result page
Response.Redirect("MultyResult.aspx", false);
}
else
{
//Redirect to the Single Result page
Response.Redirect("SingleResult.aspx", false);
}
}
else
{
//Redirect to the Message page
Session["appMessage"] = "No matching user records found or selected record's Display User Name field is empty. Try different search options, please.";
//Redirect to the Message page
Response.Redirect("MessageForm.aspx", false);
}
}
else
{
//Redirect to the Message page
Session["appMessage"] = "No matching user records found. Try different search options, please.";
//Redirect to the Message page
Response.Redirect("MessageForm.aspx", false);
}
proxy.Dispose();
}
catch (Exception ex)
{
Session["errorMessage"] = ex.Message.ToString();
//Redirect to the Error page
Response.Redirect("ErrorForm.aspx", false);
}
}
else
{
//Redirect to the Message page
Session["appMessage"] = "No matching user records found. Try different search options, please.";
//Redirect to the Message page
Response.Redirect("MessageForm.aspx", false);
}
//Redirect to the Single Result page
Response.Redirect("SingleResult.aspx", false);
}
catch (Exception ex)
{
Session["errorMessage"] = ex.Message.ToString();
//Redirect to the Error page
Response.Redirect("ErrorForm.aspx", false);
}
}
It looks like
GridView1.SelectedDataKey value == null (disappeared) by some reason?
What should I do to solve this issue?
Thanks in advance for reading this,
Dmitriy
