Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
On my Btn_Cart click as per the Entity type of the redirection of the page will be on respective form. When I book one product, first time its went through without any error. After first product add if i do back and come back again on this page and choose different type of product then error is coming on the first line of initialize the window. If first time I have choose event type product then below error is coming when i try to book another product type at the time of initialize the window.
Error :
The type initializer for 'WPFKioskDAUZ.ProductAddtoCartWindow' threw an exception.

In another scenario, if second time I do the same EntityType booking as first then there is no error.

While coming back to this screen
BookingYourTicketsWindow
and
ProductAddtoCartWindow
window i have below code on back button.
private void Btn_Back(object sender, RoutedEventArgs e)
        {
            
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            this.Close();
        }


What I have tried:

C#
private void Btn_Cart(object sender, RoutedEventArgs e)
{
	
	if (EntityType == "12")
	{
		SyncProcces();
	}
	else if (EntityType == "5")
	{
		SyncProccesEvent();
	}
}

/// 
/// in case of event 
/// 
private void SyncProccesEvent()
{
	//throw new NotImplementedException();
	BookingYourTicketsWindow eventBooking = new BookingYourTicketsWindow();
	eventBooking.Owner = Application.Current.MainWindow;
	eventBooking.ShowInTaskbar = false;
	eventBooking.Tag = "ChildWindow";
	eventBooking.Owner = this;
	eventBooking.Top = this.Top;
	eventBooking.Left = this.Left;
	eventBooking.Show();
	//eventBooking.ShowDialog();
}

/// 
/// in case of product
/// 
private void SyncProcces()
{
	//throw new NotImplementedException();
	try
	{
	   ProductAddtoCartWindow ProductBooking = new ProductAddtoCartWindow();
		ProductBooking.Owner = Application.Current.MainWindow;
		ProductBooking.ShowInTaskbar = false;
		ProductBooking.Tag = "ChildWindow";
		ProductBooking.Owner = this;
		ProductBooking.Top = this.Top;
		ProductBooking.Left = this.Left;
		ProductBooking.Show();
		//ProductBooking.ShowDialog();

		

	}
	catch (Exception ex)
	{
		//HideLoader();
		//PopUpAlert NewAlert = new PopUpAlert("No Tickets Available");
		//MainWindowPannel.Children.Add(NewAlert);
	}
}
Posted
Updated 22-Feb-17 21:55pm
v3
Comments
Graeme_Grant 23-Feb-17 2:29am    
Is there an inner exception? Please update your question with the inner exception message by clicking on Imrove question.
nipen.mehta 23-Feb-17 2:46am    
I did update in question. Its TypeInitializationException unhandled by usercode.
Graeme_Grant 23-Feb-17 2:47am    
That appears to be an outer error message... I''m expecting that there is an inner error message with more information.
nipen.mehta 23-Feb-17 2:56am    
There is no other exception apart from this. This exception is coming only when I try to add another EntityType product, as you can see I have 2 EntityType(12 and 5). The architecture is very simple. As per the EntityType I need to show different window.
Graeme_Grant 23-Feb-17 2:49am    
error is coming on the first line of initialize the window - can you please add your window constructor method as it is missing from the question - thanks.

1 solution

Okay, I have used your concept and put together a wire-frame sample for you.

Main Window Xaml:
XML
<Window x:Class="WpfWindows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfWindows"
        mc:Ignorable="d" WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel Orientation="Horizontal"
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center">
            <Button x:Name="Action1" Content="Action1" 
                    Padding="10 5" Margin="5"
                    Click="Action_Click"/>
            <Button x:Name="Action2" Content="Action2" 
                    Padding="10 5" Margin="5"
                    Click="Action_Click"/>
        </StackPanel>
    </Grid>
</Window>
Main Window Code-Behind:
C#
using System.Windows;

namespace WpfWindows
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Action_Click(object sender, RoutedEventArgs e)
        {
            Window window = null;            
            if (sender == Action1)
                window = new Window1();
            else
                window = new Window2();
            window.Owner = this;
            window.Show();
        }
    }
}
Window1 Xaml:
XML
<Window x:Class="WpfWindows.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfWindows"
        mc:Ignorable="d" WindowStartupLocation="CenterOwner"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Viewbox>
            <TextBlock Text="Window1"/>
        </Viewbox>
        <Button x:Name="Back" Grid.Row="1"
                Padding="10 5" Margin="10"
                HorizontalAlignment="Center"
                Content="BACK"
                Click="Back_Click"/>
    </Grid>
</Window>
Window1 Code-Behind:
C#
using System.Windows;

namespace WpfWindows
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
Window2 Xaml:
XML
<Window x:Class="WpfWindows.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfWindows"
        mc:Ignorable="d" WindowStartupLocation="CenterOwner"
        Title="Window2" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Viewbox>
            <TextBlock Text="Window2"/>
        </Viewbox>
        <Button x:Name="Back" Grid.Row="1"
                Padding="10 5" Margin="10"
                HorizontalAlignment="Center"
                Content="BACK"
                Click="Back_Click"/>
    </Grid>
</Window>
Window2 Code-Behind:
C#
using System.Windows;

namespace WpfWindows
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }

        private void Back_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}

As you will see, everything works as expected.

So what this is telling me is that there is a problem in your code in one of these windows, BookingYourTicketsWindow in this case.

Time to do some debugging... What I recommend is to comment out parts or all of your code to make sure the Window opens, closes, and reopens without errors. IF the issue is still there, then the problem is in your Xaml. PAir that back gradually until the error goes away, then you have identified the issue. Same goes for the code if the error is not in the Xaml, slowly re-introduce parts until the error re-appears.

Good Luck! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900