Author Topic: Using multiple DataSubscribers  (Read 2080 times)

NewControls

  • Newbie
  • *
  • Posts: 11
    • View Profile
Using multiple DataSubscribers
« on: November 09, 2017, 09:07:30 AM »
I am trying to use multiple DataSubscribers to write to an SQL database. The issue i am having is each one of my DataSubscribers are making a new row instead of writing data to the same row after the first one makes a new row. I'm not sure how to program this logic in .vb.

Here is the code i have:

Code: [Select]
    Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
        Using t As New ControlsDBDataSet1.StoreData2DataTable
            t.AddStoreData2Row(t.NewRow)
            t(0).Data = e.Values(0)

            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)

            End Using
        End Using
    End Sub

    Private Sub DataSubscriber2_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber2.DataChanged
        Using t As New ControlsDBDataSet1.StoreData2DataTable
            t.AddStoreData2Row(t.NewRow)
            t(0).State = e.Values(0)

            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)

            End Using
        End Using
    End Sub

    Private Sub DataSubscriber3_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber3.DataChanged
        Using t As New ControlsDBDataSet1.StoreData2DataTable
            t.AddStoreData2Row(t.NewRow)
            t(0).Green = e.Values(0)

            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)
            End Using
        End Using
    End Sub

I'm sure this is a simple fix.

Thanks

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Using multiple DataSubscribers
« Reply #1 on: November 09, 2017, 09:15:33 AM »
You need to declare a row variable outside the scope of the event handlers, then populate it with the data, when the last field us populated, then add the row to the table and update.

Another option is to add the row to the table as you have it now. Before calling update, check that all 3 fields have data. After the update, clear the table. This would also require you to declare the table variable outside the scope of the event handlers.
Code: [Select]
Private t as ControlsDBDataSet1.StoreData2DataTable

Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
        if t.count=0 then
            t.AddStoreData2Row(t.NewRow)
        End If

            t(0).Data = e.Values(0)

          If NOT t(0).IsDataNull AND NOT t(0).IsStateNull AND NOT t(0).isGreenNull THEN
            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)
                ta.clear
             End Using
         End If
    End Sub

NewControls

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Using multiple DataSubscribers
« Reply #2 on: November 09, 2017, 09:25:31 AM »
Awesome, thanks for the help.

Could you suggest any resources for learning basic .vb coding? or is there already a forum for that...

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Using multiple DataSubscribers
« Reply #3 on: November 09, 2017, 10:45:36 AM »
Here are some basic tutorials, but you can search the internet for VB.NET and find a lot. Or c# if you prefer the c language syntax.

http://advancedhmi.com/forum/index.php?topic=298

NewControls

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Using multiple DataSubscribers
« Reply #4 on: November 10, 2017, 03:22:43 PM »
If anyone was wondering this was the final code i used to write to a SQL database.

Code: [Select]
    Private t As New ControlsDBDataSet1.StoreData2DataTable

    Private Sub DataSubscriber1_Datareturned(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataReturned
        If t.Count = 0 Then
            t.AddStoreData2Row(t.NewRow)
        End If

        t(0).Green = e.Values(0)

        If Not t(0).IsDataNull And Not t(0).IsStateNull And Not t(0).IsGreenNull Then
            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)
                t.Clear()

            End Using
        End If
    End Sub


    Private Sub DataSubscriber2_Datareturned(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber2.DataReturned
        If t.Count = 0 Then
            t.AddStoreData2Row(t.NewRow)
        End If

        t(0).State = e.Values(0)

        If Not t(0).IsDataNull And Not t(0).IsStateNull And Not t(0).IsGreenNull Then
            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)
                t.Clear()

            End Using
        End If
    End Sub

    Private Sub DataSubscriber3_Datareturned(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber3.DataReturned
        If t.Count = 0 Then
            t.AddStoreData2Row(t.NewRow)
        End If

        t(0).Data = e.Values(0)

        If Not t(0).IsDataNull And Not t(0).IsStateNull And Not t(0).IsGreenNull Then
            Using ta As New ControlsDBDataSet1TableAdapters.StoreData2TableAdapter
                ta.Update(t)
                t.Clear()

            End Using
        End If
    End Sub

NewControls

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Using multiple DataSubscribers
« Reply #5 on: November 10, 2017, 03:38:52 PM »
Now i need to throw a wrench into the DataSubscribers mix, i need to also store a picture in the last column of the DB table. The pictures are being populated by a IP camera that is writing them to a shared folder of the PC that will be running the AdvancedHMI instance. The file location will be something like "C:\Users\Controls\Desktop\Pictures" and the files will be named something like "image-YY-MM-DD-hh-mm-ss.jpg". I just named the column of the table Pic and the data type is set to image.

If anyone could at least have some direction on this topic it would be greatly appreciated, i have seen examples of people writing a picture to a DB  column but i was not sure of what kind of implications there would be while using AdvancedHMI.

Thanks again...

(Also the current code i'm using is posted above)

Phrog30

  • Guest
Re: Using multiple DataSubscribers
« Reply #6 on: November 10, 2017, 03:48:17 PM »