Linking to an Ms-Access Data Source using Visual C++ 2008 Windows Forms Application

1.Create an Ms-Access database file using the following fields and data types:



2. Name your table as “tblApps” no quotes. Add appropriate record values for instance:



3. Name your Access database file as “dbICons” no quotes and save it in My documents. Make sure to close Ms-Access before you start Visual C++ 2008, otherwise you’ll get an “Already in use” error in the database connection part.

4. Click Start>All Programs>Microsoft C++ 2008 Express Edition.

5. Click File>New>Project>Select CLR from the Visual C++ Project Types tree view>Select Windows Forms Application from the Visual Studio installed templates pane>Enter your desired project name then click Ok.

6. Design your form as follows:



7. Just use the suggested control names above so that you will not get lost later on in this tutorial. To name a control, just select the control and change its name property value in the properties window.

8. At this moment we will now specify the name of our database that we wanted to connect to. To do that, click the Tools menu and select Connect to database:



9. Select Microsoft Access Database File (OLE DB) as a data source value then locate dbIcons and assign it as a Database filename value.



10. Click the oledbDataAdapter control from the general category of the toolbox and drag it to your form. There instances wherein the oledbdataadapter is not shown, to show it, click Tools>Choose Toolbox items>type “ole” not quotes in the filter textbox> then check everything that starts with “ole” then click the Ok button.

11. Once you have added an oledataadapter control, the data adapter configuration wizard will then come into view. If you noticed the table that you have connected previously (using step 8-9) is selected as a default value of the “what data connection should the data adapter use?” listbox. If not then click the listbox button then select “dbIcons” then click the Next button.



12. In the Choose a command type window, select the Use SQL statement radio button then click Next.

13. In the Generate SQL statement window, type “SELECT * FROM tblApps” no quotes in the blank pane then click Next>Finish.

14. Right-click>OleDbDataAdapter form the component tab>generate dataset>Ok.



15. Double-click your form then add the following in the Form_load event.
//Populates the dataset
this->oleDbDataAdapter1->Fill(this->dataSet11,"tblApps");
16. This will fill our dataset with record values from our database.

17. Bind the fieldname to an appropriate control using the following syntax:

this->objectname->databindings->add(gcnew Binding(“Property”, datasetname,”Tablename.fieldname”);

Add it below the oledbdataadapter.fill line. Your code should now look like this:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
//Populates the dataset
this->oleDbDataAdapter1->Fill(this->dataSet11,"tblApps");
//bind idlabel to the numappid fieldname
this->idlabel->DataBindings->Add(gcnew Binding("Text",this->dataSet11,"tblApps.numappid"));
//bind desclabel to the chrappdesc fieldname
this->desclabel->DataBindings->Add(gcnew Binding("Text",this->dataSet11,"tblApps.chrappdesc"));
//bind appPictureBox to the chriconurl fieldname
this->apppictureBox->DataBindings->Add(gcnew Binding("ImageLocation",this->dataSet11,"tblApps.chriconurl"));
}

18. Click the run icon to start debugging, you should see the following outputs:

A simple “Hello World” program in BlueJ

1. Click Start>All Programs>BlueJ>BlueJ.
The BlueJ Environment will then come into view.

2. Click the Project menu>New project. In the Look in Listbox, select My documents> Enter “Example” (no quotes) in the Folder Name textbox then click Create.

3. Click the New Class button>Type “Hello” (no quotes) in the Class Name textbox> Ensure that Class is selected in the class type radio buttons then click OK.



A class icon with a caption “Hello” will then appear in the BlueJ workarea.



4.Double click the Hello class icon, the following source code window will then appear:



Before creating our “Hello World” program, let us first dissect the pre-made code bit by bit. If you noticed, I’ve enabled line numbers by clicking Options>Preferences>Display line numbers, for discussion purposes.

Line 8: Is the class declaration section. It simply tells the BlueJ compiler the name of our class and to create class file based on it.
Line 9: Indicates the beginning of our class.
Line 10-12: Is the instance variable declaration section. This is where we declare the variables that will be needed in our application.
Line 16-20: Is the variable initialization section. This is where we initialize the values of our variables.
Line 28-32: Is the method declaration section. This is where we specify the actions that our application is capable of performing.
Line 34: Simply specifies the end of our class.

5. Now that we’ve understood (somehow) Blue’s code structure, let us now make our hello world program. Go to the instance variable declaration section (Line 10-12) and change “private int x;” to “private String strtext;” no quotes. The reason why we are declaring a string variable instead of the default integer variable is because we will be storing text i.e. “Hello World” to this variable in the variable initialization section.



6. Go to the variable initialization section (Line 16 to 20) and assign a “Hello World” value to our previously declared variable. This can be done by erasing ‘x=0;’ and changing it to ‘strtext=”Hello World”;’.



7. Go to the method declaration section (Line 28-32). Since we will not be accepting a numeric value from other methods nor we will be passing numeric value, change :



To



8. Your code will now look like this:

/**
* Write a description of class Hello here.
* 
* @author (your name) 
* @version (a version number or a date)
*/
public class Hello
{
// instance variables - replace the example below with your own
private String strtext;

/**
* Constructor for objects of class Hello
*/
public Hello()
{
// initialise instance variables
strtext="Hello World";
}

/**
* An example of a method - replace this comment with your own
* 
* @param  y   a sample parameter for a method
* @return     the sum of x and y 
*/
public void sampleMethod()
{
//clears the previous outputs
System.out.println("\u000c");
//retrieves the value of the strtext variable and display it on the screen.
System.out.println(strtext);
}
}
9. Click the compile button to convert your Bluej source code into bytecode.

10. Click the Close Button.

11. Create an Object based on your Hello class by right-clicking the Hello class icon>new Hello.



12. Right-click the name of the Hello object>Click your method i.e., sampleMethod.



13. You should now see an output similar to the following: