Sunday, December 18, 2011

13-Tutorial 13: Creating a Visual C++ Project from Scratch in 2010 that Calls an Access Database in Visual C++.NET 2010 (No XML) (Updated 12/11/2011

Tutorial 13: Creating a Visual C++ Project from Scratch in 2010 that Calls an Access Database in Visual C++.NET 2010 (No XML) (Updated 12/11/2011)
The Form:  Click here for a complete project with source in MSVC++.NET 2010. This program runs and compiles in 32 Bit Windows XP. The executable should run in Windows 7, 64 bit, provided the database is present, and the path adjusted to the database. But development can an issue because of the 32 bit vs. 64 bit drivers. I have a student who reports that using a older mdb database may resolve the issue with working on 64 Windows 7 using a 32 bit database, and then using the older Jet driver instead of ACE. At any rate, this program uses a database set inside it's innermost debug directory (which may have to be adjusted in Windows 7). A 2010 accdb database and a 2002 mdb database (with a working but remarked connection string) is included in the above zip files. If you run the 2002 mdb database and need the Microsoft jet driver refer to http://support.microsoft.com/kb/239114. This program does not use Odbcad32. However to make this work you will need to at least install the Access Runtime available as a free download from Microsoft. To download the Access Runtime go to http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10910   
and/or the Microsoft Access Database Engine at:
This is the best way to set up the project.  It does not rely on any special tools in MVC++.NET, and you form the link to the database right in your code.  The code works with 2008 and 20010.  Begin by creating a project called myPhoneBook and designing the form as shown below.  Note, by default the textboxes will start with a lower case (t)extBox, so you will need to rename these with an uppercase (T)extBox to use my code below:
Database and Pictures:  The way I have the database here it assumes your database is in the root of your C: drive.  If you have Windows 7 you may have to place the database in with your source files and adjust the path to your database in your source file accordingly.  Copy new pictures of people in the address book folder into the source file directory using the convention of firstname+lastname+”.jpg” .
Insert the Following Code:
#pragma once
#include <iostream> //needed for NULL

namespace mvcAccessScratchUsingOleDb {

       using namespace System;
       using namespace System::ComponentModel;
       using namespace System::Collections;
       using namespace System::Windows::Forms;
       using namespace System::Data;
       using namespace System::Drawing;
       //*********************************
       //add this
       using namespace System::Data::OleDb;

       /// <summary>
       /// Summary for Form1
       ///
       /// WARNING: If you change the name of this class, you will need to change the
       ///          'Resource File Name' property for the managed resource compiler tool
       ///          associated with all .resx files this class depends on.  Otherwise,
       ///          the designers will not be able to interact properly with localized
       ///          resources associated with this form.
       /// </summary>
       public ref class Form1 : public System::Windows::Forms::Form
       {
       public:
              static OleDbConnection ^myConnection=gcnew OleDbConnection;
              Form1(void)
              {
                    
                     InitializeComponent();
                     //using namespace System::Data::Sql;
                     //
                     //TODO: Add the constructor code here
                     //
              }

       protected:
              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              ~Form1()
              {
                     if (components)
                     {
                           delete components;
                     }
              }
       internal: System::Windows::Forms::GroupBox^  GroupBox1;
       protected:
       internal: System::Windows::Forms::Label^  label7;
       internal: System::Windows::Forms::Label^  Label1;
       internal: System::Windows::Forms::Label^  Label2;
       internal: System::Windows::Forms::TextBox^  TextBox2;
       internal: System::Windows::Forms::TextBox^  TextBox1;
       internal: System::Windows::Forms::TextBox^  TextBox3;
       internal: System::Windows::Forms::Button^  Button8;
       internal: System::Windows::Forms::Button^  Button9;
       internal: System::Windows::Forms::Button^  Button7;
       internal: System::Windows::Forms::Button^  Button6;
       internal: System::Windows::Forms::Button^  Button5;
       internal: System::Windows::Forms::Button^  Button4;
       internal: System::Windows::Forms::Button^  Button3;
       internal: System::Windows::Forms::Button^  Button2;
       internal: System::Windows::Forms::Button^  Button1;
       private: System::Windows::Forms::GroupBox^  groupBox2;
       internal:
       internal: System::Windows::Forms::Button^  Button10;
       private:
       internal: System::Windows::Forms::Label^  label9;
       internal: System::Windows::Forms::Label^  Label6;
       internal: System::Windows::Forms::Label^  Label5;
       internal: System::Windows::Forms::TextBox^  TextBox8;
       internal: System::Windows::Forms::TextBox^  TextBox7;
       internal: System::Windows::Forms::TextBox^  TextBox6;
       internal: System::Windows::Forms::Label^  Label4;
       internal: System::Windows::Forms::Label^  Label3;
       internal: System::Windows::Forms::TextBox^  TextBox5;
       internal: System::Windows::Forms::TextBox^  TextBox4;
       internal: System::Windows::Forms::ListBox^  ListBox1;
       private: System::Windows::Forms::PictureBox^  pictureBox1;
       internal:

       private:
              /// <summary>
              /// Required designer variable.
              /// </summary>
              System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
              /// <summary>
              /// Required method for Designer support - do not modify
              /// the contents of this method with the code editor.
              /// </summary>
              void InitializeComponent(void)
              {
                     System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
                     this->GroupBox1 = (gcnew System::Windows::Forms::GroupBox());
                     this->label7 = (gcnew System::Windows::Forms::Label());
                     this->Label1 = (gcnew System::Windows::Forms::Label());
                     this->Label2 = (gcnew System::Windows::Forms::Label());
                     this->TextBox2 = (gcnew System::Windows::Forms::TextBox());
                     this->TextBox1 = (gcnew System::Windows::Forms::TextBox());
                     this->TextBox3 = (gcnew System::Windows::Forms::TextBox());
                     this->Button8 = (gcnew System::Windows::Forms::Button());
                     this->Button9 = (gcnew System::Windows::Forms::Button());
                     this->Button7 = (gcnew System::Windows::Forms::Button());
                     this->Button6 = (gcnew System::Windows::Forms::Button());
                     this->Button5 = (gcnew System::Windows::Forms::Button());
                     this->Button4 = (gcnew System::Windows::Forms::Button());
                     this->Button3 = (gcnew System::Windows::Forms::Button());
                     this->Button2 = (gcnew System::Windows::Forms::Button());
                     this->Button1 = (gcnew System::Windows::Forms::Button());
                     this->groupBox2 = (gcnew System::Windows::Forms::GroupBox());
                     this->Button10 = (gcnew System::Windows::Forms::Button());
                     this->label9 = (gcnew System::Windows::Forms::Label());
                     this->Label6 = (gcnew System::Windows::Forms::Label());
                     this->Label5 = (gcnew System::Windows::Forms::Label());
                     this->TextBox8 = (gcnew System::Windows::Forms::TextBox());
                     this->TextBox7 = (gcnew System::Windows::Forms::TextBox());
                     this->TextBox6 = (gcnew System::Windows::Forms::TextBox());
                     this->Label4 = (gcnew System::Windows::Forms::Label());
                     this->Label3 = (gcnew System::Windows::Forms::Label());
                     this->TextBox5 = (gcnew System::Windows::Forms::TextBox());
                     this->TextBox4 = (gcnew System::Windows::Forms::TextBox());
                     this->ListBox1 = (gcnew System::Windows::Forms::ListBox());
                     this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
                     this->GroupBox1->SuspendLayout();
                     this->groupBox2->SuspendLayout();
                     (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
                     this->SuspendLayout();
                     //
                     // GroupBox1
                     //
                     this->GroupBox1->Controls->Add(this->label7);
                     this->GroupBox1->Controls->Add(this->Label1);
                     this->GroupBox1->Controls->Add(this->Label2);
                     this->GroupBox1->Controls->Add(this->TextBox2);
                     this->GroupBox1->Controls->Add(this->TextBox1);
                     this->GroupBox1->Controls->Add(this->TextBox3);
                     this->GroupBox1->Location = System::Drawing::Point(12, 12);
                     this->GroupBox1->Name = L"GroupBox1";
                     this->GroupBox1->Size = System::Drawing::Size(141, 116);
                     this->GroupBox1->TabIndex = 38;
                     this->GroupBox1->TabStop = false;
                     this->GroupBox1->Text = L"Enter First Last Names";
                     //
                     // label7
                     //
                     this->label7->AutoSize = true;
                     this->label7->Location = System::Drawing::Point(3, 88);
                     this->label7->Name = L"label7";
                     this->label7->Size = System::Drawing::Size(38, 13);
                     this->label7->TabIndex = 13;
                     this->label7->Text = L"Phone";
                     //
                     // Label1
                     //
                     this->Label1->AutoSize = true;
                     this->Label1->Location = System::Drawing::Point(4, 27);
                     this->Label1->Name = L"Label1";
                     this->Label1->Size = System::Drawing::Size(26, 13);
                     this->Label1->TabIndex = 7;
                     this->Label1->Text = L"First";
                     //
                     // Label2
                     //
                     this->Label2->AutoSize = true;
                     this->Label2->Location = System::Drawing::Point(3, 60);
                     this->Label2->Name = L"Label2";
                     this->Label2->Size = System::Drawing::Size(27, 13);
                     this->Label2->TabIndex = 8;
                     this->Label2->Text = L"Last";
                     //
                     // TextBox2
                     //
                     this->TextBox2->Location = System::Drawing::Point(47, 57);
                     this->TextBox2->Name = L"TextBox2";
                     this->TextBox2->Size = System::Drawing::Size(78, 20);
                     this->TextBox2->TabIndex = 6;
                     //
                     // TextBox1
                     //
                     this->TextBox1->Location = System::Drawing::Point(47, 27);
                     this->TextBox1->Name = L"TextBox1";
                     this->TextBox1->Size = System::Drawing::Size(78, 20);
                     this->TextBox1->TabIndex = 5;
                     //
                     // TextBox3
                     //
                     this->TextBox3->Location = System::Drawing::Point(46, 88);
                     this->TextBox3->Name = L"TextBox3";
                     this->TextBox3->Size = System::Drawing::Size(79, 20);
                     this->TextBox3->TabIndex = 0;
                     //
                     // Button8
                     //
                     this->Button8->Location = System::Drawing::Point(177, 229);
                     this->Button8->Name = L"Button8";
                     this->Button8->Size = System::Drawing::Size(77, 56);
                     this->Button8->TabIndex = 39;
                     this->Button8->Text = L"Delete";
                     this->Button8->UseVisualStyleBackColor = true;
                     this->Button8->Click += gcnew System::EventHandler(this, &Form1::Button8_Click);
                     //
                     // Button9
                     //
                     this->Button9->Location = System::Drawing::Point(177, 289);
                     this->Button9->Name = L"Button9";
                     this->Button9->Size = System::Drawing::Size(77, 56);
                     this->Button9->TabIndex = 38;
                     this->Button9->Text = L"Insert";
                     this->Button9->UseVisualStyleBackColor = true;
                     this->Button9->Click += gcnew System::EventHandler(this, &Form1::Button9_Click);
                     //
                     // Button7
                     //
                     this->Button7->Location = System::Drawing::Point(176, 167);
                     this->Button7->Name = L"Button7";
                     this->Button7->Size = System::Drawing::Size(77, 56);
                     this->Button7->TabIndex = 37;
                     this->Button7->Text = L"Find Wild Card Partial Match";
                     this->Button7->UseVisualStyleBackColor = true;
                     this->Button7->Click += gcnew System::EventHandler(this, &Form1::Button7_Click);
                     //
                     // Button6
                     //
                     this->Button6->Location = System::Drawing::Point(94, 289);
                     this->Button6->Name = L"Button6";
                     this->Button6->Size = System::Drawing::Size(77, 56);
                     this->Button6->TabIndex = 44;
                     this->Button6->Text = L"Close Connection";
                     this->Button6->UseVisualStyleBackColor = true;
                     this->Button6->Click += gcnew System::EventHandler(this, &Form1::Button6_Click);
                     //
                     // Button5
                     //
                     this->Button5->Location = System::Drawing::Point(93, 229);
                     this->Button5->Name = L"Button5";
                     this->Button5->Size = System::Drawing::Size(77, 56);
                     this->Button5->TabIndex = 45;
                     this->Button5->Text = L"List Sorted by Last ";
                     this->Button5->UseVisualStyleBackColor = true;
                     this->Button5->Click += gcnew System::EventHandler(this, &Form1::Button5_Click);
                     //
                     // Button4
                     //
                     this->Button4->Location = System::Drawing::Point(94, 166);
                     this->Button4->Name = L"Button4";
                     this->Button4->Size = System::Drawing::Size(77, 56);
                     this->Button4->TabIndex = 43;
                     this->Button4->Text = L"List Sorted By First";
                     this->Button4->UseVisualStyleBackColor = true;
                     this->Button4->Click += gcnew System::EventHandler(this, &Form1::Button4_Click);
                     //
                     // Button3
                     //
                     this->Button3->Location = System::Drawing::Point(10, 291);
                     this->Button3->Name = L"Button3";
                     this->Button3->Size = System::Drawing::Size(77, 56);
                     this->Button3->TabIndex = 42;
                     this->Button3->Text = L"List Records";
                     this->Button3->UseVisualStyleBackColor = true;
                     this->Button3->Click += gcnew System::EventHandler(this, &Form1::Button3_Click);
                     //
                     // Button2
                     //
                     this->Button2->Location = System::Drawing::Point(11, 228);
                     this->Button2->Name = L"Button2";
                     this->Button2->Size = System::Drawing::Size(77, 56);
                     this->Button2->TabIndex = 41;
                     this->Button2->Text = L"Count Records";
                     this->Button2->UseVisualStyleBackColor = true;
                     this->Button2->Click += gcnew System::EventHandler(this, &Form1::Button2_Click);
                     //
                     // Button1
                     //
                     this->Button1->Location = System::Drawing::Point(11, 165);
                     this->Button1->Name = L"Button1";
                     this->Button1->Size = System::Drawing::Size(77, 56);
                     this->Button1->TabIndex = 40;
                     this->Button1->Text = L"Make Connection";
                     this->Button1->UseVisualStyleBackColor = true;
                     this->Button1->Click += gcnew System::EventHandler(this, &Form1::Button1_Click);
                     //
                     // groupBox2
                     //
                     this->groupBox2->Controls->Add(this->Button10);
                     this->groupBox2->Controls->Add(this->label9);
                     this->groupBox2->Controls->Add(this->Label6);
                     this->groupBox2->Controls->Add(this->Label5);
                     this->groupBox2->Controls->Add(this->TextBox8);
                     this->groupBox2->Controls->Add(this->TextBox7);
                     this->groupBox2->Controls->Add(this->TextBox6);
                     this->groupBox2->Controls->Add(this->Label4);
                     this->groupBox2->Controls->Add(this->Label3);
                     this->groupBox2->Controls->Add(this->TextBox5);
                     this->groupBox2->Controls->Add(this->TextBox4);
                     this->groupBox2->Location = System::Drawing::Point(159, 12);
                     this->groupBox2->Name = L"groupBox2";
                     this->groupBox2->Size = System::Drawing::Size(273, 148);
                     this->groupBox2->TabIndex = 46;
                     this->groupBox2->TabStop = false;
                     this->groupBox2->Text = L"Modify";
                     //
                     // Button10
                     //
                     this->Button10->Location = System::Drawing::Point(187, 52);
                     this->Button10->Name = L"Button10";
                     this->Button10->Size = System::Drawing::Size(77, 56);
                     this->Button10->TabIndex = 25;
                     this->Button10->Text = L"Modify";
                     this->Button10->UseVisualStyleBackColor = true;
                     this->Button10->Click += gcnew System::EventHandler(this, &Form1::Button10_Click);
                     //
                     // label9
                     //
                     this->label9->AutoSize = true;
                     this->label9->Location = System::Drawing::Point(3, 120);
                     this->label9->Name = L"label9";
                     this->label9->Size = System::Drawing::Size(81, 13);
                     this->label9->TabIndex = 24;
                     this->label9->Text = L"Modified Phone";
                     //
                     // Label6
                     //
                     this->Label6->AutoSize = true;
                     this->Label6->Location = System::Drawing::Point(4, 95);
                     this->Label6->Name = L"Label6";
                     this->Label6->Size = System::Drawing::Size(70, 13);
                     this->Label6->TabIndex = 23;
                     this->Label6->Text = L"Modified Last";
                     //
                     // Label5
                     //
                     this->Label5->AutoSize = true;
                     this->Label5->Location = System::Drawing::Point(6, 72);
                     this->Label5->Name = L"Label5";
                     this->Label5->Size = System::Drawing::Size(69, 13);
                     this->Label5->TabIndex = 22;
                     this->Label5->Text = L"Modified First";
                     //
                     // TextBox8
                     //
                     this->TextBox8->Location = System::Drawing::Point(85, 118);
                     this->TextBox8->Name = L"TextBox8";
                     this->TextBox8->Size = System::Drawing::Size(95, 20);
                     this->TextBox8->TabIndex = 21;
                     //
                     // TextBox7
                     //
                     this->TextBox7->Location = System::Drawing::Point(85, 94);
                     this->TextBox7->Name = L"TextBox7";
                     this->TextBox7->Size = System::Drawing::Size(95, 20);
                     this->TextBox7->TabIndex = 20;
                     //
                     // TextBox6
                     //
                     this->TextBox6->Location = System::Drawing::Point(86, 69);
                     this->TextBox6->Name = L"TextBox6";
                     this->TextBox6->Size = System::Drawing::Size(95, 20);
                     this->TextBox6->TabIndex = 19;
                     //
                     // Label4
                     //
                     this->Label4->AutoSize = true;
                     this->Label4->Location = System::Drawing::Point(2, 47);
                     this->Label4->Name = L"Label4";
                     this->Label4->Size = System::Drawing::Size(27, 13);
                     this->Label4->TabIndex = 18;
                     this->Label4->Text = L"Last";
                     //
                     // Label3
                     //
                     this->Label3->AutoSize = true;
                     this->Label3->Location = System::Drawing::Point(4, 25);
                     this->Label3->Name = L"Label3";
                     this->Label3->Size = System::Drawing::Size(26, 13);
                     this->Label3->TabIndex = 17;
                     this->Label3->Text = L"First";
                     //
                     // TextBox5
                     //
                     this->TextBox5->Location = System::Drawing::Point(86, 44);
                     this->TextBox5->Name = L"TextBox5";
                     this->TextBox5->Size = System::Drawing::Size(95, 20);
                     this->TextBox5->TabIndex = 16;
                     //
                     // TextBox4
                     //
                     this->TextBox4->Location = System::Drawing::Point(86, 20);
                     this->TextBox4->Name = L"TextBox4";
                     this->TextBox4->Size = System::Drawing::Size(95, 20);
                     this->TextBox4->TabIndex = 15;
                     //
                     // ListBox1
                     //
                     this->ListBox1->FormattingEnabled = true;
                     this->ListBox1->Location = System::Drawing::Point(259, 174);
                     this->ListBox1->Name = L"ListBox1";
                     this->ListBox1->Size = System::Drawing::Size(328, 173);
                     this->ListBox1->TabIndex = 47;
                     //
                     // pictureBox1
                     //
                     this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
                     this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"pictureBox1.Image")));
                     this->pictureBox1->Location = System::Drawing::Point(448, 16);
                     this->pictureBox1->Name = L"pictureBox1";
                     this->pictureBox1->Size = System::Drawing::Size(139, 144);
                     this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
                     this->pictureBox1->TabIndex = 48;
                     this->pictureBox1->TabStop = false;
                     //
                     // Form1
                     //
                     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
                     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
                     this->ClientSize = System::Drawing::Size(599, 357);
                     this->Controls->Add(this->pictureBox1);
                     this->Controls->Add(this->ListBox1);
                     this->Controls->Add(this->groupBox2);
                     this->Controls->Add(this->Button8);
                     this->Controls->Add(this->GroupBox1);
                     this->Controls->Add(this->Button9);
                     this->Controls->Add(this->Button4);
                     this->Controls->Add(this->Button7);
                     this->Controls->Add(this->Button1);
                     this->Controls->Add(this->Button6);
                     this->Controls->Add(this->Button2);
                     this->Controls->Add(this->Button5);
                     this->Controls->Add(this->Button3);
                     this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
                     this->Name = L"Form1";
                     this->Text = L"PhoneBook Program";
                     this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
                     this->GroupBox1->ResumeLayout(false);
                     this->GroupBox1->PerformLayout();
                     this->groupBox2->ResumeLayout(false);
                     this->groupBox2->PerformLayout();
                     (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
                     this->ResumeLayout(false);

              }
#pragma endregion
       private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                      }
private: System::Void Button1_Click(System::Object^  sender, System::EventArgs^  e)
               {
// try this for Windows 7 if the first connection string doesn't work
//myConnection->ConnectionString = "Provider =Microsoft.Jet.OLEDB.4.0; Data Source =c:\\PhoneBook.mdb; Persist Security Info =False;";


                     
                      try
                      {
                  myConnection->Open();
                  ListBox1->Items->Clear();
                  ListBox1->Items->Add("Connection made");
                             myConnection->Close();
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");
                      }
               }
private: System::Void Button2_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      try
                      {
                 String ^queryString = "select count(*) as RecordCount from Table1";
                            String ^result="";
                            //OleDbDataReader ^reader;// =gcnew OleDbDataReader ;//command.ExecuteReader();
               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {
                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteScalar()->ToString();
                                    ListBox1->Items->Add(result);
                                    myConnection->Close();
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");
                      }
               }
private: System::Void Button3_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      try
                      {
                 String ^queryString = "select * from Table1";
                            OleDbDataReader ^result;//
                
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {
                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteReader();
                                    while( result->Read())
                                    {
                          ListBox1->Items->Add(result->GetValue(1)->ToString() + " " + result->GetValue(2)->ToString()+ " " + result->GetValue(3)->ToString());
                                    }
                                    result->Close();
                                    myConnection->Close();
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");
                      }
               }
private: System::Void Button4_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      try
                      {
                 String ^queryString = "select * from Table1 order by firstName";
                            OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();    
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {
                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteReader();
                                    while( result->Read())
                                    {
                           ListBox1->Items->Add( result->GetValue(2)->ToString()+" " + result->GetValue(1)->ToString() + " " + result->GetValue(3)->ToString());
                                    }
                      result->Close();
                                 myConnection->Close();
                       }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");;
                      }
               }
private: System::Void Button5_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      try
                      {
                 String ^queryString = "select * from Table1 order by lastName";
                            OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {

                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteReader();
                                    while( result->Read())
                                    {
                           ListBox1->Items->Add(result->GetValue(1)->ToString() + " " + result->GetValue(2)->ToString()+ " " + result->GetValue(3)->ToString());
                                    }
                                    result->Close();
                                    myConnection->Close();
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");;
                      }
               }

private: System::Void Button6_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      ListBox1->Items->Clear();

                            //if( myConnection->Equals(NULL) )
                         //if( myConnection->Equals(nullptr) )
                      if( myConnection->Equals(nullptr ))
                            {
                                   ListBox1->Items->Add("No Connection to close");
                                   return;
                            }
                      else
                      {
                            ListBox1->Items->Add("Connection closed");
                            myConnection->Close();
                      }
               }
private: System::Void Button7_Click(System::Object^  sender, System::EventArgs^  e)
               {
                        int found = -1;
               String ^firstName="";
                        String ^lastName="";
                        String ^sqlFind="";
                        firstName=TextBox1->Text->Trim();
                        lastName=TextBox2->Text->Trim();
                        String ^FLname="";
                        if (TextBox1->Text->Trim()== "" && TextBox2->Text->Trim() == "")
                           {
                        MessageBox::Show("Enter First Name or last name or first Name and last Name");
                        return;
                           }
                         try
                         {
                 String ^queryString = "select * from Table1 Where firstName like '%" + firstName + "%' and lastName like '%" + lastName + "%'";
                            OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {

                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteReader();
                                    while( result->Read())
                                    {
                           ListBox1->Items->Add(result->GetValue(1)->ToString() + " " + result->GetValue(2)->ToString()+ " " + result->GetValue(3)->ToString());
                                            TextBox3->Text=result->GetValue(3)->ToString();
                                            FLname=result->GetValue(2)->ToString()+result->GetValue(1)->ToString()+".jpg";
                                            found = found + 1;
                                    }
                                    result->Close();
                                    myConnection->Close();
                                    if (found == -1)
                                    {
                                           ListBox1->Items->Add("No records found");
                                    }
                                    else
                                    if(IO::File::Exists(FLname))
                                    {    //checks if jpg (picture) file present in directory                            
                                           pictureBox1->Image=Image::FromFile(FLname);
                                    }
                                    else
                                    {
                                           pictureBox1->Image=Image::FromFile("Default.jpg");
                                    }
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");;
                      }
               }
private: System::Void Button8_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      ListBox1->Items->Clear();
               String ^sqlDelete="";
               String ^firstName="";
               String ^lastName="";
                        int result=-1;
                        firstName=TextBox1->Text->Trim();
                        lastName=TextBox2->Text->Trim();
                        if (TextBox1->Text->Trim()== "" && TextBox2->Text->Trim() == "")
                           {
                        MessageBox::Show("Enter First Name and Last name ");
                        return;
                           }
                         try
                         {
                 String ^queryString = "delete from Table1 Where lastName = '" + lastName + "' and firstName = '" + firstName + "'";
                            //OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {

                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteNonQuery();
                                    myConnection->Close();
                                    if( result== 0)
                                    {
                         ListBox1->Items->Add("No records deleted!");
                                    }
                                    else
                                    {
                          ListBox1->Items->Add(result.ToString() + " record(s) deleted!");
                                    }
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");;
                      }
               }
private: System::Void Button9_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      ListBox1->Items->Clear();
               String ^sqlDelete="";
               String ^firstName="";
               String ^lastName="";
                        String ^phone="";
                        int result=-1;
                        firstName=TextBox1->Text->Trim();
                        lastName=TextBox2->Text->Trim();
                        phone=TextBox3->Text->Trim();
                        if (TextBox1->Text->Trim()== "" && TextBox2->Text->Trim() == "" && TextBox3->Text->Trim() == "")
                           {
                        MessageBox::Show("Enter First Name, Last name, and Phone ");
                        return;
                           }
                         try
                         {
                 String ^queryString = "INSERT INTO Table1(firstName, lastName, phone) VALUES('" + firstName + "', '" + lastName + "', '" + phone+ "')";
                            //OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(queryString, myConnection); 
                            myConnection->Open();
                             if(myConnection->State !=ConnectionState::Open)
                             {

                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                              else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteNonQuery();
                                    myConnection->Close();
                                    if( result== 0)
                                    {
                         ListBox1->Items->Add("No records inserted!");
                                    }
                                    else
                                    {
                          ListBox1->Items->Add( "Record inserted!");
                                    }
                             }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");
                      }
               }
private: System::Void Button10_Click(System::Object^  sender, System::EventArgs^  e)
               {
                      OleDbDataReader ^result;
               String ^sqlDelete="";
               String ^firstName="";
                        String ^newFirst="";
               String ^lastName="";
                        String ^newLast="";
                        String ^phone="";
                        String ^newPhone="";
                        String ^sqlFind;                  
                        String  ^sqlUpdate="";
                        int upDateCount=-1;
                        //int result=-1;
                     ListBox1->Items->Clear();
                        firstName=TextBox4->Text->Trim();
                        lastName=TextBox5->Text->Trim();

                        newFirst=TextBox6->Text->Trim();
                        newLast=TextBox7->Text->Trim();
                        newPhone=TextBox8->Text->Trim();

                        sqlFind= "select * from Table1 Where lastName = '" + lastName + "' and firstName = '" + firstName + "'";
                        if (TextBox4->Text->Trim()== "" && TextBox5->Text->Trim() == ""
                              && TextBox6->Text->Trim() == ""&& TextBox7->Text->Trim() == ""
                              && TextBox8->Text->Trim() == "")
                           {
                        MessageBox::Show("Fill in Modification Fields");
                        return;
                           }
                         try
                         {
                                  //sqlFind
                 //String ^queryString = "INSERT INTO Table1(firstName, lastName, phone) VALUES('" + firstName + "', '" + lastName + "', '" + phone+ "')";
                            //OleDbDataReader ^result;// =gcnew OleDbDataReader ;//command.ExecuteReader();               
                            myConnection->ConnectionString = "Provider =Microsoft.ACE.OLEDB.12.0; Data Source =c:\\PhoneBook.accdb; Persist Security Info =False;";
                 OleDbCommand ^SqlCommand=gcnew OleDbCommand(sqlFind, myConnection); 
                            myConnection->Open();
                             //MessageBox::Show(sqlFind);
                             if(myConnection->State !=ConnectionState::Open)
                             {

                                    ListBox1->Items->Clear();
                                    ListBox1->Items->Add("Error");
                                    myConnection->Close();
                             }
                             else
                             {
                                    ListBox1->Items->Clear();
                                    result = SqlCommand->ExecuteReader();
                                    if ( !result->HasRows)
                                     {
                                            //MessageBox::Show(sqlFind);
                                            ListBox1->Items->Add("No records found to modify");
                                            //myConnection->Close();
                                     }
                                    else
                                    {
                                           result->Close();
                                           sqlUpdate = "update Table1 set firstName = '" + newFirst + "', lastName = '" + newLast +  "', phone = '" + newPhone + "' where firstName = '" + firstName + "' and lastName = '" + lastName + "'";
                                           MessageBox::Show(sqlUpdate);
                                           OleDbCommand ^sqlUpdateCommand=gcnew OleDbCommand(sqlUpdate, myConnection);
                                           upDateCount=sqlUpdateCommand->ExecuteNonQuery();
                                           if(upDateCount == 0)
                                           {
                                ListBox1->Items->Add("No records updated!");
                                           }
                                           else
                                           {
                                ListBox1->Items->Add("Record updated!");           
                                        }                                          
                                    }
                                     myConnection->Close();
                                    }
                      }
                      catch (Exception ^e)
                      {
                MessageBox::Show(e->Message, "Error");;
                      }
               }
};
}



 (Back) 

0 comments:

Post a Comment