Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. How to close & clear the oledbconnection on form2()_load?

How to close & clear the oledbconnection on form2()_load?

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++databasevisual-studio
2 Posts 2 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paramu1973
    wrote on last edited by
    #1

    Hi, I am using vs-2015. Managed C++/Cli Windows Forms Applications. I have Form1-MdiContainer & Form2. From Form1_Button_Click() accessing Form2. In Form2_Load() I have OLEDBConnection. For When I press Form1-Button_Click(), First time it's working good. Also when I re-click() the same Form1-Button() the whole application is getting closed. I analysed that when I close the OLEDBConnection, the whole program is also getting closed. And the same code working good in c#. I can't understand my mistakes..! Thanks. Note:- For First time when I click the Form1-Button no problem it's working good. But for re-click() whole application is getting closed(). Thanks Again My Codes

    Form1-MdiContainer()
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    Form2^ MyStForm = Form2::GetForm(true, this);
    MyStForm->MdiParent = this;
    MyStForm->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
    MyStForm->Dock = DockStyle::Fill;
    MyStForm->Show();
    }
    Form2-Top side
    public ref class Form2 : public System::Windows::Forms::Form
    {
    public: static Form2^ Form2::_instance = nullptr;
    public: static Form2^ Form2::GetForm(bool IsMDIChild, System::Windows::Forms::Form^ MyInstFrm) {
    if (_instance == nullptr)
    _instance = gcnew Form2();
    if (_instance->IsDisposed)
    _instance = gcnew Form2();
    if (IsMDIChild)
    _instance->MdiParent = MyInstFrm;
    return _instance;
    }
    blah..blah...blah...
    }

    Form2-Load()
    private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
    String^ MyStrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + MyExcelFile + "; Extended Properties=\"Excel 12.0; HDR=YES; OLE DB Services=-1; \"";
    String^ MyExcelFile="C:\\Students\\Names.xlsx";
    String^ MyExcelSheet = "[Sheet1$]";
    String^ MySQLSelect = "select * from " + MyExcelSheet;
    System::Data::OleDb::OleDbConnection^ Cn1 = nullptr;
    Cn1 = gcnew System::Data::OleDb::OleDbConnection();
    Cn1->ConnectionString = MyStrConn;
    System::Data::OleDb::OleDbCommand^ MyCmd = nullptr;
    MyCmd = gcnew System::Data::OleDb::OleDbCommand();
    MyCmd->CommandText = MySQLSelect;
    MyCmd->Connection = Cn1;
    Cn1->Open();
    System::Data::OleDb::OleDbDataAdapter^ Da1 = gcnew System::Data::OleDb::OleDbDataAdapter(MyCmd);
    Da1->Fill(MyDataTable);
    Cn1->Close(); //????????????????? getting stuck
    if (Cn1 != nullptr){
    MyCmd->Cancel();
    }
    //Cn1->ConnectionString = nullptr;
    //Cn1->ReleaseObjectPool();
    MyCmd = nullptr;
    Cn1 = nullptr

    L 1 Reply Last reply
    0
    • P Paramu1973

      Hi, I am using vs-2015. Managed C++/Cli Windows Forms Applications. I have Form1-MdiContainer & Form2. From Form1_Button_Click() accessing Form2. In Form2_Load() I have OLEDBConnection. For When I press Form1-Button_Click(), First time it's working good. Also when I re-click() the same Form1-Button() the whole application is getting closed. I analysed that when I close the OLEDBConnection, the whole program is also getting closed. And the same code working good in c#. I can't understand my mistakes..! Thanks. Note:- For First time when I click the Form1-Button no problem it's working good. But for re-click() whole application is getting closed(). Thanks Again My Codes

      Form1-MdiContainer()
      private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
      Form2^ MyStForm = Form2::GetForm(true, this);
      MyStForm->MdiParent = this;
      MyStForm->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
      MyStForm->Dock = DockStyle::Fill;
      MyStForm->Show();
      }
      Form2-Top side
      public ref class Form2 : public System::Windows::Forms::Form
      {
      public: static Form2^ Form2::_instance = nullptr;
      public: static Form2^ Form2::GetForm(bool IsMDIChild, System::Windows::Forms::Form^ MyInstFrm) {
      if (_instance == nullptr)
      _instance = gcnew Form2();
      if (_instance->IsDisposed)
      _instance = gcnew Form2();
      if (IsMDIChild)
      _instance->MdiParent = MyInstFrm;
      return _instance;
      }
      blah..blah...blah...
      }

      Form2-Load()
      private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {
      String^ MyStrConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + MyExcelFile + "; Extended Properties=\"Excel 12.0; HDR=YES; OLE DB Services=-1; \"";
      String^ MyExcelFile="C:\\Students\\Names.xlsx";
      String^ MyExcelSheet = "[Sheet1$]";
      String^ MySQLSelect = "select * from " + MyExcelSheet;
      System::Data::OleDb::OleDbConnection^ Cn1 = nullptr;
      Cn1 = gcnew System::Data::OleDb::OleDbConnection();
      Cn1->ConnectionString = MyStrConn;
      System::Data::OleDb::OleDbCommand^ MyCmd = nullptr;
      MyCmd = gcnew System::Data::OleDb::OleDbCommand();
      MyCmd->CommandText = MySQLSelect;
      MyCmd->Connection = Cn1;
      Cn1->Open();
      System::Data::OleDb::OleDbDataAdapter^ Da1 = gcnew System::Data::OleDb::OleDbDataAdapter(MyCmd);
      Da1->Fill(MyDataTable);
      Cn1->Close(); //????????????????? getting stuck
      if (Cn1 != nullptr){
      MyCmd->Cancel();
      }
      //Cn1->ConnectionString = nullptr;
      //Cn1->ReleaseObjectPool();
      MyCmd = nullptr;
      Cn1 = nullptr

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This might help re: the data adapter.

      Quote:

      This overload of the Fill method does not implicitly call Close on the ADO object when the fill operation is complete. Therefore, always call Close when you are finished using ADO Recordset or Record objects. This makes sure that the underlying connection to a data source is released in a timely manner, and also prevents possible access violations because of unmanaged ADO objects being reclaimed by garbage collection when existing references still exist.

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups