Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Part 3: Creating the application to view the data with VB.NET

Expand / Collapse
 

Part 3: Creating the application to view the data with VB.NET


In part 2, we created a working application to read telemetry from sensors connected to a USB-2408, write the telemetry or data to the screen in various ways, and write it out to the database we created in part 1.

Now, in part 3, we will create the application to read the data back out of the database.  It will be a mirror image of the part 2 application, but no data is collected here it will just show what was collected and stored to the database.  It will operate like dual ported memory.  This is all in preparation for the next part where we will distribute the two applications so as to have one application (DataCollectionToDatabase) on one computer, and the other app (DatabaseToDataDisplay) on another computer in a different part of the building.

Again, launch Visual Basic, and start a new project.  We will call it “DatabaseToDataDisplay.”  Resize your form to 639, 358.

Add a MenuStrip, DataGridView, Timer, OpenFileDialog, Thermometer, 2 AnalogMeters, an LED, 2 buttons, and 7 labels.  Rearrange and resize the objects so that your form design looks like this:

Change the following properties:

Of all the labels, only 2 need to be renamed:

The one under the thermometer:  lblTempValue

The one to the right of Time:  lblTime

All labels should be set to Autosize = True

From the Solution explorer, right click on Form1.vb, and select View Code.  Above Public Class Form1 insert the following:

Imports System.Data.OleDb

Imports System.Runtime.InteropServices

Imports System.IO

 

Next, add the variable declarations:

'For database

Dim sConnectionString As String

Public objConn As New OleDbConnection()

Dim ds As New DataSet()

Dim da As OleDb.OleDbDataAdapter

Dim sql As String

 

Open the external database and read in the data:

There is a lot less going on in this application, just about all of it in happening in the Timer, so open the Timer1_Tick() event, and paste in the following:

Try  'This goes out to the database

 

    'How to open an oleDB data base

    sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data  Source=c:\Users\Public\Documents\MCCGenericOdbc.mdb"   'for oleDB

    objConn = New OleDbConnection(sConnectionString)

    objConn.Open()

 

    'Obtain a DataRow object from the DataTable.

    da = New OleDb.OleDbDataAdapter("SELECT * FROM TestData", objConn)

    da.FillSchema(ds,SchemaType.Source, "TestData")

    da.Fill(ds, "TestData")

 

    objConn.Close()

 

Catch ex As OleDbException

    Timer1.Enabled = False

    objConn.Close()

    MsgBox(ex.Message.ToString(), MsgBoxStyle.Information,"Error Message")

End Try

 

 

Try

    Dim MyDataScan = ds.Tables("TestData").Rows(0)

 

    lblTime.Text = MyDataScan(1).ToString

 

    Thermometer1.TempValue = Convert.ToDouble(MyDataScan(2))

    lblTempValue.Text = MyDataScan(2).ToString + "°C"

 

    amPressure.Value = Convert.ToDouble(MyDataScan(3))

 

    amTach.Value = Convert.ToDouble(MyDataScan(4)/ 100)

 

    Dim r As Boolean = Convert.ToBoolean(Val(MyDataScan(5)))

    Led1.Value = r

 

Catch ex As OleDbException

    Timer1.Enabled = False

    MsgBox(ex.Message.ToString(), MsgBoxStyle.Information,"Error Message")

End Try

 

   

As with the previous app, we want to open the database.  But now we want to obtain existing data from the TestData table. When we read in the entire table we use the Fill Schema and Fill commands.  Read in the first row from the table and parse it (Dim MyDataScan = ds.Tables("TestData").Rows(0)).  The data will automatically be placed into the array, ‘MyDataScan()’ for dissemination to the various labels and objects in this application.  All the data is stored as strings, so there are data conversions going on in the code above.

The app could come up running, but for now let’s use a Start/Stop button, and so from the Form View, double click on “Start” button, and add the following to the btnStartStop_Click() event:

If btnStartStop.Text = "Start"Then

    btnStartStop.Text = "Stop"

    Timer1.Enabled = True

Else

    btnStartStop.Text = "Start"

    Timer1.Enabled = False

    objConn.Close()

End If

 

The If/Then/Else above, alternates the Text property of the button between ‘Start’ and ‘Stop’, but when IF condition is true we also enable or start the timer.  When the IF condition is false, we stop the timer, and close the link to the data base.

Add a graceful way to exit the program:

From the Form view, regarding the MenuStrip1, click on File, and then double click on Exit, and add the following code:

Timer1.Enabled = False

End

Because this is Windows, and there is always more than one way to do anything, go back to the Form view, double click on the End button, and paste the same syntax there.

We are still missing a couple items, but that is as far as we need to go for this section.

The two applications are now complete.  Let’s run both of them from within the VisualBasic.NET environments.  Here is what they look like, running in concert:

Despite the different types of meters used in the two applications, you can see the two applications are sharing the data.  What you can’t see, is the top app is sending data to table “TestData” of database “MccGenericOdbc.mdb”, and the bottom app is reading data from the table “TestData” of database “MccGenericOdbc.mdb”. Since the two apps are running their own timers, data reads and writes are happening ‘on demand.’ For these applications, the data is one second delayed, which is why the data on the two forms is not exactly the same. There will always be some delay due to timers, Windows OS, and network traffic.

The completed application is attached to this Knowledge base article.



Rate this Article:

Attachments


DatabaseToDataDisplay.zip DatabaseToDataDisplay.zip (46.69 KB, 597 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50619

Last Modified:10/13/2016 3:03:47 PM

Article has been viewed 2,318 times.

Options