Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 23rd, 2008, 12:39 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 121
Thanks: 1
Thanked 0 Times in 0 Posts
Default GridView.SelectedDataKey turning into null

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace Gridview field if null with new field Indo77 ASP.NET 2.0 Basics 1 June 18th, 2007 06:22 AM
Turning XML into HTML [email protected] XSLT 2 June 8th, 2006 05:20 PM
Turning Siblings into Children wolfie78uk XSLT 3 December 22nd, 2005 08:33 AM
Turning strings into bits...help patpicos BOOK: Beginning Java 2 2 March 29th, 2005 06:09 AM
turning ' into ’ bigmish Classic ASP Basics 4 August 21st, 2004 11:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.