General Category > Support Questions

Using multiple DataSubscribers

(1/2) > >>

NewControls:
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: ---    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
--- End code ---

I'm sure this is a simple fix.

Thanks

Archie:
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: ---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

--- End code ---

NewControls:
Awesome, thanks for the help.

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

Archie:
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:
If anyone was wondering this was the final code i used to write to a SQL database.


--- Code: ---    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

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version