tags:

views:

210

answers:

2

I am getting the following exception:

"Nullable object must have a value"

Everything was working when I was using

StartupURI="MainWindow.xaml"

but I wanted to implement a login screen so I changed this to

Startup="Application_Startup" 

and then created the following method in App.xaml.cs:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        UpdateAccounts();
        bool result = true;
        ///*
        LoginWindow login = new LoginWindow();            
        result = login.ShowDialog().Value;
        /* */

        if (!result)
        {
            return;
        }

        MainWindow window = new MainWindow();
        bool main = window.ShowDialog().Value;
    }

Does anyone have any idea what is going on? Or any suggestions on what is the best practice for implementing login interface.

The exception is being thrown at

bool main = window.ShowDialog().Value;

Exception Stack Trace:

   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Nullable`1.get_Value()

My MainWindow.xaml was my default window that was being loaded when app started, and it worked prefectly fine. It has a lot of code inside it. A couple listboxes, a couple combo boxes, month calendar. It also connects to an access database.

To me it seems like my Window is return from ShowDialog() straight away, without letting me set DialogResult.

Update: I don't get an exception if i change my code to:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        UpdateAccounts();
        bool result = true;
        ///*
        LoginWindow login = new LoginWindow();            
        result = login.ShowDialog().Value;
        /* */

        if (!result)
        {
            return;
        }

        MainWindow window = new MainWindow();
        window.ShowDialog();
    }

but I also don't get my main window show up, and the app just closes after logging in.

+1  A: 

From the message that you receive it seems like your login.ShowDialog() is returning null.

ema
thanks for the tip, but my window isn't even being shown. My MainWindow.xaml was my default window previously, is there anything i need to set for it to show correctly? And be shown until it closes?
LnDCobra
I've not tryed but I think that Application_Startup is not the right event to do this since you block the execution with the LoginWindow and nothing will be displayed. Try to move that code in another place (after the startup event)
ema
A: 

Found a temporary fix, which isn't the best option but it works.

App.xaml:

<Application x:Class="Power_Scheduler.App"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Startup="Application_Startup"
   ShutdownMode="OnExplicitShutdown">
</Application>

Had to add ShutdownMode="OnExplicitShutdown"

in my code I used the Show() method rather than ShowDialog().

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        UpdateAccounts();
        bool result = true;
        ///*
        LoginWindow login = new LoginWindow();            
        result = login.ShowDialog().Value;
        /* */

        if (!result)
        {
            return;
        }

        MainWindow window = new MainWindow();
        window.Show();
    }

Which keeps the application running and shows the main window. And in the MainWindow, I subscribed to the Closed event and added the following code:

    private void Window_Closed(object sender, EventArgs e)
    {
        Application.Current.Shutdown(0);
    }      

THanks for your help everyone,

LnDCobra