Issue with Closing Login Form and Opening Dialog Based on Authentication Result

31 Views Asked by At

I've been working on a login form which I'm creating with C++ and SQL server database.
The main login form reads everything correctly from the database but I get an error starting a new dialog box to show the authentication is successful.

Below is a snippet from MyForm.h

private: System::Void btnOK_Click(System::Object^ sender, System::EventArgs^ e) {
    String^ email = this->tbEmail->Text;
    String^ password = this->tbPassword->Text;

    if (email->Length == 0 || password->Length == 0)
    {
        MessageBox::Show("Please enter Email and Password","Email and Password Error",MessageBoxButtons::OK);
        return;
    }
    try
    {
        String^ connstring = "Data Source = ASUS\\SQLEXPRESS;Initial Catalog = DBMS_TA;Integrated Security = True;Encrypt = False";
        SqlConnection sqlConn(connstring);
        sqlConn.Open();
        

        String^ query = "SELECT * FROM kinshukuser WHERE email = @email AND password = @password";
        SqlCommand command(query, % sqlConn);
        command.Parameters->AddWithValue("@email", email);
        command.Parameters->AddWithValue("@password", password);

        SqlDataReader^ reader = command.ExecuteReader();
        if (reader->Read())
        {
            user = gcnew User;
            user->value = 1;
//this is where my login form closes and a dialog box should open which i have defined in another .cpp file
            this->Close();
        }
        else
        {
            MessageBox::Show("Email or password is incorrect.","Email or Password Error",MessageBoxButtons::OK);

        }
    }
    catch(Exception^ ex)
    {
        MessageBox::Show("Failed to connect to Database"+ex->Message, "Database Connection Error", MessageBoxButtons::OK);
    }
}

Below I have the two files (User.h and Program.ccp), which are both included in the main file.

User.h

#pragma once

using namespace System;

public ref class User {
public:
    int value;
};

Program.cpp

#include "MyForm.h"

using namespace System;
using namespace System::Windows::Forms;

void main(array<String^>^ args)
{
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    DBMSTA::MyForm zp;

    zp.ShowDialog();
    User^ user = zp.user;
    if (user!=nullptr)
    {
        MessageBox::Show("Successful Authentication of User",
            "Authentication Dialog",MessageBoxButtons::OK);
    }
    else
    {
        MessageBox::Show("Authentication Failed!","Program.cpp"
            ,MessageBoxButtons::OK);
    }

}

I tried changing the main from void main to int main did nothing.

It's a CLR project.

0

There are 0 best solutions below