Thread: VS C++ db with ado.net

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    VS C++ db with ado.net

    C++ database
    With Visual Studio.Net's C++ windows form I have an Access database.
    The db(access), the form, the dataadapter, and the connection is done.
    The dataset is to be programmed through the program not the wizard, even if I do it by the wizard it still won't display the db.
    3 - textboxes (customer id, last name, first name)
    supposed to display the customers table from the database and then display in a datagrid only that customers orders (from the Orders table in the db)
    navigate through with previous and next buttons.
    there is a class Form1



    Code:
    //Form1.h
    
    #pragma once
    
    
    namespace cassign4
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    		public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    
    		}
    
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Data::OleDb::OleDbDataAdapter *  dbOrds;
    	private: System::Data::OleDb::OleDbCommand *  oleDbSelectCommand1;
    	private: System::Data::OleDb::OleDbCommand *  oleDbInsertCommand1;
    	private: System::Data::OleDb::OleDbCommand *  oleDbUpdateCommand1;
    	private: System::Data::OleDb::OleDbCommand *  oleDbDeleteCommand1;
    	private: System::Data::OleDb::OleDbConnection *  oleDbConnection1;
    	private: System::Windows::Forms::Label *  lblCID;
    	private: System::Windows::Forms::Label *  lblLName;
    	private: System::Windows::Forms::Label *  lblFName;
    	private: System::Windows::Forms::TextBox *  txtCustID;
    	private: System::Windows::Forms::TextBox *  txtLName;
    	private: System::Windows::Forms::TextBox *  txtFName;
    
    	private: System::Windows::Forms::Button *  btnPrev;
    	private: System::Windows::Forms::Button *  btnNext;
    	private: System::Data::DataSet * dscustomers;
    	private: System::Windows::Forms::DataGrid *  dgOrds;
    	private: System::Windows::Forms::Button *  btnDisp;
    
    	//private: System::Data::DataSet * dscustomers;
    
    
    
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->dbOrds = new System::Data::OleDb::OleDbDataAdapter();
    			this->oleDbDeleteCommand1 = new System::Data::OleDb::OleDbCommand();
    			this->oleDbConnection1 = new System::Data::OleDb::OleDbConnection();
    			this->oleDbInsertCommand1 = new System::Data::OleDb::OleDbCommand();
    			this->oleDbSelectCommand1 = new System::Data::OleDb::OleDbCommand();
    			this->oleDbUpdateCommand1 = new System::Data::OleDb::OleDbCommand();
    			this->lblCID = new System::Windows::Forms::Label();
    			this->lblLName = new System::Windows::Forms::Label();
    			this->lblFName = new System::Windows::Forms::Label();
    			this->txtCustID = new System::Windows::Forms::TextBox();
    			this->txtLName = new System::Windows::Forms::TextBox();
    			this->txtFName = new System::Windows::Forms::TextBox();
    			this->btnPrev = new System::Windows::Forms::Button();
    			this->btnNext = new System::Windows::Forms::Button();
    			this->dgOrds = new System::Windows::Forms::DataGrid();
    			this->btnDisp = new System::Windows::Forms::Button();
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dgOrds))->BeginInit();
    			this->SuspendLayout();
    			// 
    			// dbOrds
    			// 
    			this->dbOrds->DeleteCommand = this->oleDbDeleteCommand1;
    			this->dbOrds->InsertCommand = this->oleDbInsertCommand1;
    			this->dbOrds->SelectCommand = this->oleDbSelectCommand1;
    			System::Data::Common::DataTableMapping* __mcTemp__1[] = new System::Data::Common::DataTableMapping*[1];
    			System::Data::Common::DataColumnMapping* __mcTemp__2[] = new System::Data::Common::DataColumnMapping*[3];
    			__mcTemp__2[0] = new System::Data::Common::DataColumnMapping(S"custid", S"custid");
    			__mcTemp__2[1] = new System::Data::Common::DataColumnMapping(S"custln", S"custln");
    			__mcTemp__2[2] = new System::Data::Common::DataColumnMapping(S"custfn", S"custfn");
    			__mcTemp__1[0] = new System::Data::Common::DataTableMapping(S"Table", S"customers", __mcTemp__2);
    			this->dbOrds->TableMappings->AddRange(__mcTemp__1);
    			this->dbOrds->UpdateCommand = this->oleDbUpdateCommand1;
    			// 
    			// oleDbDeleteCommand1
    			// 
    			this->oleDbDeleteCommand1->CommandText = S"DELETE FROM customers WHERE (custid = \?) AND (custfn = \? OR \? IS NULL AND custfn " 
    				S"IS NULL) AND (custln = \? OR \? IS NULL AND custln IS NULL)";
    			this->oleDbDeleteCommand1->Connection = this->oleDbConnection1;
    			this->oleDbDeleteCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custid", System::Data::OleDb::OleDbType::Integer, 0, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custid", System::Data::DataRowVersion::Original, 0));
    			this->oleDbDeleteCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custfn", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custfn", System::Data::DataRowVersion::Original, 0));
    			this->oleDbDeleteCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custfn1", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custfn", System::Data::DataRowVersion::Original, 0));
    			this->oleDbDeleteCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custln", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custln", System::Data::DataRowVersion::Original, 0));
    			this->oleDbDeleteCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custln1", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custln", System::Data::DataRowVersion::Original, 0));
    			// 
    			// oleDbConnection1
    			// 
    			this->oleDbConnection1->ConnectionString = S"Jet OLEDB:Registry Path=;Data Source=\"H:\\My Documents\\assign4c.mdb\";Jet OLEDB:Sys" 
    				S"tem database=;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:Don't Copy Locale o" 
    				S"n Compact=False;User ID=Admin;Provider=\"Microsoft.Jet.OLEDB.4.0\";Jet OLEDB:Datab" 
    				S"ase Locking Mode=1;Jet OLEDB:SFP=False;Jet OLEDB:Encrypt Database=False;Jet OLED" 
    				S"B:Create System Database=False;Jet OLEDB:Compact Without Replica Repair=False;Je" 
    				S"t OLEDB:Global Partial Bulk Ops=2;Mode=Share Deny None;Extended Properties=;Jet " 
    				S"OLEDB:Engine Type=5";
    			// 
    			// oleDbInsertCommand1
    			// 
    			this->oleDbInsertCommand1->CommandText = S"INSERT INTO customers(custid, custln, custfn) VALUES (\?, \?, \?)";
    			this->oleDbInsertCommand1->Connection = this->oleDbConnection1;
    			this->oleDbInsertCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custid", System::Data::OleDb::OleDbType::Integer, 0, S"custid"));
    			this->oleDbInsertCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custln", System::Data::OleDb::OleDbType::VarWChar, 50, S"custln"));
    			this->oleDbInsertCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custfn", System::Data::OleDb::OleDbType::VarWChar, 50, S"custfn"));
    			// 
    			// oleDbSelectCommand1
    			// 
    			this->oleDbSelectCommand1->CommandText = S"SELECT custid, custln, custfn FROM customers";
    			this->oleDbSelectCommand1->Connection = this->oleDbConnection1;
    			// 
    			// oleDbUpdateCommand1
    			// 
    			this->oleDbUpdateCommand1->CommandText = S"UPDATE customers SET custid = \?, custln = \?, custfn = \? WHERE (custid = \?) AND (c" 
    				S"ustfn = \? OR \? IS NULL AND custfn IS NULL) AND (custln = \? OR \? IS NULL AND cust" 
    				S"ln IS NULL)";
    			this->oleDbUpdateCommand1->Connection = this->oleDbConnection1;
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custid", System::Data::OleDb::OleDbType::Integer, 0, S"custid"));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custln", System::Data::OleDb::OleDbType::VarWChar, 50, S"custln"));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"custfn", System::Data::OleDb::OleDbType::VarWChar, 50, S"custfn"));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custid", System::Data::OleDb::OleDbType::Integer, 0, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custid", System::Data::DataRowVersion::Original, 0));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custfn", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custfn", System::Data::DataRowVersion::Original, 0));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custfn1", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custfn", System::Data::DataRowVersion::Original, 0));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custln", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custln", System::Data::DataRowVersion::Original, 0));
    			this->oleDbUpdateCommand1->Parameters->Add(new System::Data::OleDb::OleDbParameter(S"Original_custln1", System::Data::OleDb::OleDbType::VarWChar, 50, System::Data::ParameterDirection::Input, false, (System::Byte)0, (System::Byte)0, S"custln", System::Data::DataRowVersion::Original, 0));
    			// 
    			// lblCID
    			// 
    			this->lblCID->Location = System::Drawing::Point(24, 16);
    			this->lblCID->Name = S"lblCID";
    			this->lblCID->Size = System::Drawing::Size(72, 23);
    			this->lblCID->TabIndex = 0;
    			this->lblCID->Text = S"Customer ID:";
    			// 
    			// lblLName
    			// 
    			this->lblLName->Location = System::Drawing::Point(32, 56);
    			this->lblLName->Name = S"lblLName";
    			this->lblLName->Size = System::Drawing::Size(64, 23);
    			this->lblLName->TabIndex = 1;
    			this->lblLName->Text = S"Last Name:";
    			// 
    			// lblFName
    			// 
    			this->lblFName->Location = System::Drawing::Point(32, 96);
    			this->lblFName->Name = S"lblFName";
    			this->lblFName->Size = System::Drawing::Size(64, 23);
    			this->lblFName->TabIndex = 2;
    			this->lblFName->Text = S"First Name:";
    			// 
    			// txtCustID
    			// 
    			this->txtCustID->Location = System::Drawing::Point(128, 16);
    			this->txtCustID->Name = S"txtCustID";
    			this->txtCustID->Size = System::Drawing::Size(176, 20);
    			this->txtCustID->TabIndex = 3;
    			this->txtCustID->Text = S"";
    			// 
    			// txtLName
    			// 
    			this->txtLName->Location = System::Drawing::Point(128, 56);
    			this->txtLName->Name = S"txtLName";
    			this->txtLName->Size = System::Drawing::Size(176, 20);
    			this->txtLName->TabIndex = 4;
    			this->txtLName->Text = S"";
    			// 
    			// txtFName
    			// 
    			this->txtFName->Location = System::Drawing::Point(128, 96);
    			this->txtFName->Name = S"txtFName";
    			this->txtFName->Size = System::Drawing::Size(176, 20);
    			this->txtFName->TabIndex = 5;
    			this->txtFName->Text = S"";
    			// 
    			// btnPrev
    			// 
    			this->btnPrev->Location = System::Drawing::Point(416, 24);
    			this->btnPrev->Name = S"btnPrev";
    			this->btnPrev->Size = System::Drawing::Size(75, 32);
    			this->btnPrev->TabIndex = 7;
    			this->btnPrev->Text = S"Previous";
    			this->btnPrev->Click += new System::EventHandler(this, btnPrev_Click);
    			// 
    			// btnNext
    			// 
    			this->btnNext->Location = System::Drawing::Point(416, 72);
    			this->btnNext->Name = S"btnNext";
    			this->btnNext->Size = System::Drawing::Size(75, 32);
    			this->btnNext->TabIndex = 8;
    			this->btnNext->Text = S"Next";
    			this->btnNext->Click += new System::EventHandler(this, btnNext_Click);
    			// 
    			// dgOrds
    			// 
    			this->dgOrds->DataMember = S"";
    			this->dgOrds->HeaderForeColor = System::Drawing::SystemColors::ControlText;
    			this->dgOrds->Location = System::Drawing::Point(88, 136);
    			this->dgOrds->Name = S"dgOrds";
    			this->dgOrds->Size = System::Drawing::Size(352, 128);
    			this->dgOrds->TabIndex = 9;
    			// 
    			// btnDisp
    			// 
    			this->btnDisp->Location = System::Drawing::Point(240, 288);
    			this->btnDisp->Name = S"btnDisp";
    			this->btnDisp->Size = System::Drawing::Size(75, 32);
    			this->btnDisp->TabIndex = 10;
    			this->btnDisp->Text = S"Display";
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->ClientSize = System::Drawing::Size(536, 333);
    			this->Controls->Add(this->btnDisp);
    			this->Controls->Add(this->dgOrds);
    			this->Controls->Add(this->btnNext);
    			this->Controls->Add(this->btnPrev);
    			this->Controls->Add(this->txtFName);
    			this->Controls->Add(this->txtLName);
    			this->Controls->Add(this->txtCustID);
    			this->Controls->Add(this->lblFName);
    			this->Controls->Add(this->lblLName);
    			this->Controls->Add(this->lblCID);
    			this->Name = S"Form1";
    			this->Text = S"Customer Orders";
    			this->Load += new System::EventHandler(this, Form1_Load);
    			(__try_cast<System::ComponentModel::ISupportInitialize *  >(this->dgOrds))->EndInit();
    			this->ResumeLayout(false);
    			
    //			this->dscustomers = new DataSet("customers");
    
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    			 {
    			 }
    
    private: System::Void btnPrev_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    
    		 }
    
    private: System::Void btnNext_Click(System::Object *  sender, System::EventArgs *  e)
    		 {
    		 }
    
    };
    }
    
    
    //Form1.cpp
    
    #include "stdafx.h"
    #include "Form1.h"
    #include <windows.h>
    
    using namespace cassign4;
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
    	Application::Run(new Form1());
    	return 0;
    }
    Thank you for any help that anyone can give me

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't cross post.

    gg
    Last edited by Codeplug; 11-27-2003 at 11:40 AM.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    posting twice

    I didn't realize I was in the C board when I wanted into the C++ board and when I went to delete the C board I couldn't...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with bootstrap
    By abachler in forum Linux Programming
    Replies: 0
    Last Post: 01-23-2009, 10:41 PM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  4. if is faster than switch?
    By skorman00 in forum C++ Programming
    Replies: 32
    Last Post: 03-06-2004, 01:15 PM
  5. Need help comparing db field values in c++
    By misha in forum C++ Programming
    Replies: 18
    Last Post: 07-09-2003, 09:37 AM