Unhalted exception has occured in your application

0

Napisalem juz swoj pierwszy program w C#. Program wspoldziala z MSSQL.
Kiedy kompiluje i uruchamiam wszystko dziala OK.
Kiedy natomiast probuje go uruchomic jako samodzielna aplikacje to zarowno
wersja Debug jak i Release wywala mi taki blad:
16326885454f699bb50f031.png

po rozwinieciu:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

         * Exception Text **************

System.NullReferenceException: Object reference not set to an instance of an object.
at testoptima1.Form1.OnResize(Object sender, EventArgs e) in E:\VisualC#\testoptima1\testoptima1\Form1.cs:line 32
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Form.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

         * Loaded Assemblies **************

mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.261 (RTMGDR.030319-2600)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

testoptima1
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///E:/VisualC%23/testoptima1/testoptima1/bin/Debug/testoptima1.exe

System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.235 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.261 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll

System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.233 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll

System.Data
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.237 (RTMGDR.030319-2300)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll

System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.233 built by: RTMGDR
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll

System.Transactions
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Transactions/v4.0_4.0.0.0__b77a5c561934e089/System.Transactions.dll

System.EnterpriseServices
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.EnterpriseServices/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll

System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll

         * JIT Debugging **************

To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration> <system.windows.forms jitdebugging="true" /> </configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

O co moze chodzic?

0

KOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOD (plik Form1.cs).

0

No zaczyna sie tak:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        MSDataBase.ReadStartSet(); <- funkcja klasy odpowiedzialnej za polaczenie z baza danych - wczytuje z pliku tekstowego
        dataGridView1.DataSource = MSDataBase._LoadList("SELECT Id, Nazwisko, Imie, Zwolnienie AS ZW, Data_Zatrudnienia AS Od, Data_Zwolnienia AS Do FROM PK_Archiwum1_Osoby");
        dataGridView1.Columns[0].Width = 50;
        dataGridView1.Columns[1].Width = 150;
        dataGridView1.Columns[3].Width = 50;
        dataGridView1.Columns[4].Width = 80;
        dataGridView1.Columns[5].Width = 80;
        dataGridView1.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dataGridView1.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dataGridView1.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    }

    private void OnResize(object sender, EventArgs e)
    {
        dataGridView1.Height = 3 * Form1.ActiveForm.Height/4;  <- TO JEST LINIA 32
        dataGridView1.Width = Form1.ActiveForm.Width/2 - 30;
        dataGridView2.Height = Form1.ActiveForm.Height / 2;
        dataGridView2.Width = Form1.ActiveForm.Width / 2 - 60;
        dataGridView2.Location = new Point(dataGridView1.Location.X + Form1.ActiveForm.Width / 2, dataGridView2.Location.Y);
        dataGridView3.Width = dataGridView2.Width;
        dataGridView3.Height = Form1.ActiveForm.Height / 2 - 100;
        dataGridView3.Location = new Point(dataGridView2.Location.X, dataGridView2.Location.Y + dataGridView2.Height + 20);
    }

    private void OnLoad(object sender, EventArgs e)
    {                           
        dataGridView1.Height = Form1.ActiveForm.Height - 4 * Menu.Height;
        dataGridView1.Width = Form1.ActiveForm.Width / 2 - 30;
        dataGridView1.Location = new Point(dataGridView1.Location.X, dataGridView1.Location.Y + Menu.Height);
        dataGridView2.Height = Form1.ActiveForm.Height / 2;
        dataGridView2.Width = Form1.ActiveForm.Width / 2 - 60;
        dataGridView2.Location = new Point(dataGridView1.Location.X + Form1.ActiveForm.Width / 2, dataGridView2.Location.Y + Menu.Height);
        dataGridView3.Width = dataGridView2.Width;
        dataGridView3.Height = dataGridView1.Height - dataGridView2.Height - 20;
        dataGridView3.Location = new Point(dataGridView2.Location.X, dataGridView2.Location.Y + dataGridView2.Height + 20);
    }

i wiecej na starcie sie nic nie dzieje (chyba) ...

MSDataBase.ReadStartSet(); <- funkcja klasy odpowiedzialnej za polaczenie z baza danych - wczytuje z pliku tekstowego dane do utworzenia connection stringa:
public static void ReadStartSet()
{
string szSrcLine;
ArrayList szContents = new ArrayList();
FileStream fsInput = new FileStream("start.set", FileMode.Open,
FileAccess.Read);
StreamReader srInput = new StreamReader(fsInput);
while ((szSrcLine = srInput.ReadLine()) != null)
{
// dołączanie do tablicy
szContents.Add(szSrcLine);
}
srInput.Close();
fsInput.Close();
server = szContents[0].ToString();
database = szContents[1].ToString();
user = szContents[2].ToString();
password = szContents[3].ToString();
user_id = szContents[4].ToString();
}

        dataGridView1.DataSource = MSDataBase._LoadList("SELECT Id, Nazwisko, Imie, Zwolnienie AS ZW, Data_Zatrudnienia AS Od, Data_Zwolnienia AS Do FROM PK_Archiwum1_Osoby"); <- funkcja klasy odpowiedzialnej za polaczenie z baza danych - ustala zrodlo danych dla datagridview:

public static BindingSource _LoadList(string question)
{
BindingSource bindSource = new BindingSource();
SqlDataAdapter dataAdapter = new SqlDataAdapter();

        using (SqlConnection objConn = CreateConnection())
        {
            dataAdapter = new SqlDataAdapter(question, objConn);
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            DataTable table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            bindSource.DataSource = table;

        }

        return bindSource;
    }
0

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activeform.aspx

A Form that represents the currently active form, or null if there is no active form.

Czyli zapewne ActiveForm jest null. Sprawdź to po prostu ifem

0

sprawdze - ok. Ale nawet jesli jest taki blad to dlaczego kiedy wciskam w srodowisku Visual Studio F5 - to program uruchamia mi sie bez problemow (kompilacja tez nie wyrzuca zadnych bledow)?

Problem pojawia sie dopiero, kiedy zamkne srodowisko i proboje uruchomic program klikajac na jego ikonke.

Dodam jeszcze, ze jak przy tym komunikacie klikne Continue - to program uruchamia sie i dalej dziala normalnie.
Tylko ten komunikat ...

0

No dodalem
if (Form1.ActiveForm.Enabled == true)

I teraz wyjatek zmienil sie na taki:

         * Exception Text **************

System.NullReferenceException: Object reference not set to an instance of an object.
at testoptima1.Form1.OnResize(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Form.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

0

Geez, if(Form1.ActiveForm != null) costam.

0

Usunalem calkowicie funkcje OnResize() i problem zniknal...
Tylko dlaczego w ogole wystapil? Jesli nawet byl blad logiczny w kodzie to dlaczego nie zostal wylapany podczas kompilacji
badz uruchamiania w srodowisku?

Wstawiajac ten warunek:
if(Form1.ActiveForm != null) problem tez juz nie wystepuje. Dzieki.

Jednak na powyzsze pytanie wciaz nie znam odpowiedz....

0

Proces uruchomiony w Visual Studio nie zachowuje się identycznie z tym, który został uruchomiony bez debuggera. Tyle ci wystarczy.

W każdym razie, po co w ogóle z tej właściwości korzystasz? Jeżeli chcesz ustawić właściwości tej konkretnej formy to dlaczego nie ustawiasz ich bezpośrednio (przez jawną / niejawną referencję this)?

0

Chce, zeby zmiana rozmiaru Form1 miala odzwierciedlenie w rozmiarze 3 roznych datagridview znajdujacych sie na niej.
Czyli - jesli uzytkownik zmniejsza okno - zmniejszaja sie tez te 3 gridy.
A o co chodzi z tym this? Moglbys podac przyklad?

1 użytkowników online, w tym zalogowanych: 0, gości: 1