title: How to loop through each control inside the panel to get value from it and save the value from screen to database
body: <p>How to loop through each control inside the panel to get value from it and save the value from screen to database</p>
  
  <p>Here on this screen i am dynamically creating controls (Label, UltraDateTimeEditor and Button)</p>
  
  <p>I am holding some datetime values in datetimeeditor control. Now in the below screen i have save button.</p>
  
  <p>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.</p>
  
  <p>My code is below.</p>
  
  <pre><code> Private Function LoadFlow()
  
      Me.Panel1.Controls.Clear()
  
      If dtDataTable.Rows.Count &gt; 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 &gt;= 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
  </code></pre>
  
  <p>My datatable used for this code looks like this</p>
  
  <p><a href="https://i.stack.imgur.com/enPh2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/enPh2.png" alt="enter image description here"></a></p>
  
  <p>My screen looks like this.</p>
  
  <p><a href="https://i.stack.imgur.com/0RG9A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0RG9A.png" alt="enter image description here"></a></p>
  
  <p>My update SQL Stored Procedure Query</p>
  
  <pre><code>    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
  </code></pre>