How to pass through each command inside the panel to get a value and save the value of the screen to the database

advertisements

How to loop through each control inside the panel to get value from it and save the value from screen to database

Here on this screen i am dynamically creating controls (Label, UltraDateTimeEditor and Button)

I am holding some datetime values in datetimeeditor control. Now in the below screen i have save button.

So when i click save i need to get datetime value from screen and save to database. So i need to loop through each value and save accordingly with its respective row in datatabase.

My code is below.

 Private Function LoadFlow()

    Me.Panel1.Controls.Clear()

    If dtDataTable.Rows.Count > 0 AndAlso dtDataTable IsNot Nothing Then

        Dim n = dtDataTable.Rows.Count

        For i As Integer = 0 To n - 1
            'Create label
            Dim label As New Label()
            label.Text = dtDataTable.Rows(i)("CHECKPOINTNAME").ToString()
            'Position label on screen
            label.Left = 50
            label.Top = (i + 1) * 30
            label.Width = 70

            'Create UltraDateTimeEditor
            Dim dtpicker = New UltraDateTimeEditor
            dtpicker.Name = "Date" + i.ToString()
            dtpicker.MaskInput = "{date} hh:mm"
            dtpicker.MaskDisplayMode = MaskMode.IncludeBoth
            dtpicker.SpinButtonDisplayStyle = Infragistics.Win.ButtonDisplayStyle.Always
            dtpicker.Left = 120
            dtpicker.Tag = i 'Set this as we will use this for comparison
            dtpicker.Top = (i + 1) * 30

            If Not IsDBNull(dtDataTable.Rows(i)("CheckPointTimeValue")) Then
                dtpicker.Value = dtDataTable.Rows(i)("CheckPointTimeValue")
            Else
                dtpicker.Value = Nothing
            End If

            Dim button As New Button()
            'Position textbox on screen
            button.Tag = i 'Set this as we will use this for comparison
            button.Text = "Now"
            button.Left = 290
            button.Top = (i + 1) * 30
            'Add controls to form
            Me.Panel1.ClientArea.Controls.Add(label)
            Me.Panel1.ClientArea.Controls.Add(dtpicker)
            Me.Panel1.ClientArea.Controls.Add(button)

            AddHandler button.Click, AddressOf UpdateTime
        Next
    End If
    Return Nothing
End Function

Private Sub UpdateTime(sender As System.Object, e As System.EventArgs)
        Dim nButton As Button = DirectCast(sender, Button)
        If nButton IsNot Nothing AndAlso nButton.Tag >= 0 Then
            For Each ctrl In Me.Panel1.Controls.OfType(Of UltraDateTimeEditor)()
                If ctrl.Tag = nButton.Tag Then
                    ctrl.Value = DateTime.Now
                    Exit Sub
                End If
            Next
        End If

    End Sub

My datatable used for this code looks like this

My screen looks like this.

My update SQL Stored Procedure Query

    CREATE PROCEDURE [TAMS].[pu_InsertCheckPointTimes]
        (
           ,@CheckPointTimeID INT
           ,@CheckPointTimeValue DATETIME = NULL
           ,@LastModifiedUser VARCHAR(128)
        )
    AS
    BEGIN

            IF(@CheckPointTimeID = 0)

    BEGIN

        INSERT INTO [TAMS].[CheckPointTimes]
                   ([CheckPointTimeValue]
                   ,[LastModifiedUser])

             VALUES
                   (@CheckPointTimeValue
                       ,@LastModifiedUser)

    END
  ELSE
    BEGIN
        UPDATE [TAMS].[CheckPointTimes]
        SET [CheckPointTimeValue] = @CheckPointTimeValue
                  ,[LastModifiedUser] = @LastModifiedUser
        WHERE [CheckPointTimeID] = @CheckPointTimeID
    END

    END


Instead assigning an integer to a tag of each control you put inside a panel try to assign it a checkpointid value and then you can do something like this:

    For Each o As Object In Panel1.Controls
        If TypeOf (o) Is UltraDateTimeEditor Then
           Dim datePicker as UltraDateTimeEditor = o
           Dim id as Integer = datePicker.Tag
           Dim date as DateTime = datePicker.value
           //save your values

        End If
    Next