0

My application collects username and password from mainpage.xaml and makes an httprequest to the server. If the response from the server is PASS then i want the control to be navigated to another xaml page. I used the following code

       if ((rs1[0].Trim()).Equals("PASS"))
       {
          NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
                     }
       else
       {
           MessageBox.Show(rs);
       }

where rs1 is an array of strings.

But I get a NullReferenceException. Plz suggest any alternate way. Thanks in advance.

The full code is as follows:

namespace aquila1
{
    public partial class MainPage : PhoneApplicationPage
    {
        static string username;
        static string password;
        static string rs;

        static NavigationService ns = new NavigationService();

        // Constructor
        public MainPage()
        {
            InitializeComponent();


        }

        private static ManualResetEvent allDone = new ManualResetEvent(true);

       private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
       {
           username = textbox1.Text;
           password = textbox2.Text;
           System.Diagnostics.Debug.WriteLine(username);
           System.Diagnostics.Debug.WriteLine(password);

           HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://60.243.245.181/fms_tracking/php/mobile_login.php?username=" + username + "&password=" + password);


           request.ContentType = "application/x-www-form-urlencoded";

           // Set the Method property to 'POST' to post data to the URI.
           request.Method = "POST";

           // start the asynchronous operation
           request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);

           // Keep the main thread from continuing while the asynchronous 
           // operation completes. A real world application 
           // could do something useful such as updating its user interface. 
           allDone.WaitOne();



       }


        private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {

            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = request.EndGetRequestStream(asynchronousResult);

            // Console.WriteLine("Please enter the input data to be posted:");
            string postData = username + "+" + password;
            System.Diagnostics.Debug.WriteLine(postData);
            // Convert the string into a byte array. 
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);

            // Write to the request stream.
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }

        private static void GetResponseCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            Stream streamResponse = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(streamResponse);
           string responseString = streamRead.ReadToEnd();
            rs = responseString; 
            System.Diagnostics.Debug.WriteLine(responseString);
            System.Diagnostics.Debug.WriteLine("@@@@@");
            System.Diagnostics.Debug.WriteLine(rs);

            // Close the stream object
            streamResponse.Close();
            streamRead.Close();

            // Release the HttpWebResponse
            response.Close();
            move2();


            allDone.Set();

        }


       private static void move2()
       {

           string[] rs1 = rs.Split(':');
           if ((rs1[0].Trim()).Equals("PASS"))
           {

               ns.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
           }
           else
           {
               MessageBox.Show(rs);
           }
       }



    }
}
user2090226
  • 97
  • 1
  • 9

3 Answers3

1

I'm pretty sure you cannot initialize a new NaviagtionService(), you can only get an instance from PageInstance.NavigationService property, in Silverlight.

(See http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice(v=vs.95).aspx)

You can get the current page using (Application.Current.RootVisual as Frame).Content as PhoneApplicationPage, so you can get PageInstance.NavigationService on it.

But a simpler way is to call Navigate() on Frame directly, (Application.Current.RootVisual as Frame).Navigate(...) will just be working.

yume_chan
  • 858
  • 9
  • 22
0

My psychic debugging abilities tell me that you have wrote the code inside either: - a static constructor - constructor - a method called before calling OnNavigatedTo

In general, NavigationService is null before you enter the OnNavigatedTo method. You might want to move that logic from its current position.

Where is it located now anyway?

Toni Petrina
  • 7,014
  • 1
  • 25
  • 34
0

@user2090226 Do you have your SecondPage.xaml on the root? Just asking you because people make silly mistakes. Otherwise try using uri to be in full path. And preferably check your navigation. You created an object instead of factory methods. Ex:

new Uri("/MyProject;component/AllPageFolder/SecondPage.xaml",Urikind.Relative);

For Pages created in the root itself take this example:

NavigationService.Navigate(new Uri("/Photography;component/SlideShow.xaml", UriKind.Relative));
Mani
  • 1,364
  • 14
  • 33