Results 1 to 5 of 5

Thread: how to update data into the database from dynamically created controls

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    86

    Question how to update data into the database from dynamically created controls

    hi,
    Iam developing a application where a form displays dynamically created contols and displays data in it from the database.
    this form allows the user to edit records and any changes made to it should be updated in to the database.
    since the contols are dynamically created iam not able to figure out how to refer to them and save the changes.
    can anyone guide me here please.i would really appreciate the help.
    thanks.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    86

    Re: how to update data into the database from dynamically created controls

    well to be more clearer i would like to provide more information on my form.
    this form is displaying only the text boxes right now.so all the input would have to be updated from these dynamic textboxes to the database.
    this is my code in the forms load event:



    Code:
    VB Code:
    1. Private Sub frmDataEntry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\customer.mdb")
    4.         Dim cn1 As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\customer.mdb")
    5.         Dim cmd As OleDbCommand
    6.         Dim dr As OleDbDataReader
    7.         Dim dr1 As OleDbDataReader
    8.         Dim str As String
    9.         Dim str1 As String
    10.         Dim cmd1 As OleDbCommand
    11.         Dim txtBox As TextBox
    12.         Dim labl As Label
    13.         Dim pt As Point
    14.         Dim pt1 As Point
    15.         Dim cnt As Integer
    16.         Dim cnt1 As Integer
    17.         Dim lablsiz As Size = New Size(150, 20)
    18.         Dim txtsiz As Size = New Size(300, 20)
    19.         Dim i As Integer
    20.         Dim ds As DataSet
    21.  
    22.         Try
    23.             If cn.State = ConnectionState.Closed Then
    24.                 cn.Open()
    25.             End If
    26.  
    27.             cn1.Open()
    28.             str1 = "SELECT FldAn FROM tblCustomFieldAnswers WHERE AcctNum='" & m_stracctnum1 & "' And CustID = " & m_intcustnum1 & ""
    29.             cmd1 = New OleDbCommand(str1, cn1)
    30.             dr1 = cmd1.ExecuteReader
    31.  
    32.  
    33.             str = "SELECT FieldName FROM tblCustomFields WHERE AcctNum='" & m_stracctnum1 & "'And CustID = " & m_intcustnum1 & ""
    34.             cmd = New OleDbCommand(str, cn)
    35.             dr = cmd.ExecuteReader
    36.  
    37.  
    38.             If dr1.HasRows = False Then
    39.  
    40.                 While dr.Read
    41.                     pt1 = New Point(10, cnt1 + 10)
    42.                     labl = New Label
    43.                     labl.Text = dr(i) & ":"
    44.                     labl.TextAlign = ContentAlignment.MiddleRight
    45.                     labl.Location = pt1
    46.                     labl.Size = lablsiz
    47.                     Me.Controls.Add(labl)
    48.                     pt = New Point(175, cnt + 10)
    49.                     txtBox = New TextBox
    50.                     txtBox.Location = pt
    51.                     txtBox.Size = txtsiz
    52.                     Me.Controls.Add(txtBox)
    53.  
    54.                     cnt = cnt + (txtBox.Height + 15)
    55.                     cnt1 = cnt1 + (labl.Height + 15)
    56.                 End While
    57.  
    58.             Else
    59.                 While dr.Read
    60.                     pt1 = New Point(10, cnt1 + 10)
    61.                     labl = New Label
    62.                     labl.Text = dr(i) & ":"
    63.                     labl.TextAlign = ContentAlignment.MiddleRight
    64.                     labl.Location = pt1
    65.                     labl.Size = lablsiz
    66.                     Me.Controls.Add(labl)
    67.                     While dr1.Read
    68.                         pt = New Point(175, cnt + 10)
    69.                         txtBox = New TextBox
    70.                         txtBox.Location = pt
    71.                         txtBox.Size = txtsiz
    72.                         Me.Controls.Add(txtBox)
    73.                         txtBox.Text = dr1(i)
    74.                         cnt = cnt + (txtBox.Height + 15)
    75.                     End While
    76.                     cnt1 = cnt1 + (labl.Height + 15)
    77.                 End While
    78.             End If
    79.  
    80.         Catch ex As OleDbException
    81.  
    82.             MessageBox.Show(ex.ToString())
    83.  
    84.         End Try
    85.         If cn.State <> ConnectionState.Closed Then
    86.             cn.Close()
    87.         End If
    88.         If cn1.State <> ConnectionState.Closed Then
    89.             cn1.Close()
    90.         End If
    91.  
    92.     End Sub
    so now when they make changes and hit the edit button the changes made to the text of any textboxes should be stored back to FldAn in customfieldanswers.
    please help!!
    thanks,

  3. #3
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: how to update data into the database from dynamically created controls

    When you dynamically create your controls, it's a good idea to always name them so that you can refer back to it by name later.
    For example, if you create a textbox in the form load event like this
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, _
    2.                                  ByVal e As System.EventArgs) _
    3.                                  Handles MyBase.Load
    4.         Dim txtBox As TextBox
    5.         txtBox = New TextBox
    6.         txtBox.Name = "myTextBox"
    7.         txtBox.Size = New Size(128, 20)
    8.         txtBox.Location = New Point(208, 24)
    9.         txtBox.Text = "This is a test"
    10.         Me.Controls.Add(txtBox)
    11.     End Sub
    Then later you can access it via its name
    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, _
    2.                                    ByVal e As System.EventArgs) _
    3.                                    Handles Button2.Click
    4.         'Loop thru the form's controls and look for the name
    5.         For Each ctrl As Control In Me.Controls
    6.             If ctrl.Name = "myTextBox" Then
    7.                 'If find it, you need to cast it back to whatever control it is
    8.                 'In this case, it's a TextBox
    9.                 MsgBox(CType(ctrl, TextBox).Text)
    10.             End If
    11.         Next
    12.     End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    86

    Re: how to update data into the database from dynamically created controls

    hi,
    thanks for the suggestion .so this is what i did:

    VB Code:
    1. Private Sub frmDataEntry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.  
    3.         Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\customer.mdb")
    4.         Dim cn1 As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Application.StartupPath & "\customer.mdb")
    5.         Dim cmd As OleDbCommand
    6.         Dim dr As OleDbDataReader
    7.         Dim dr1 As OleDbDataReader
    8.         Dim str As String
    9.         Dim str1 As String
    10.         Dim cmd1 As OleDbCommand
    11.         Dim txtBox As TextBox
    12.         Dim labl As Label
    13.         Dim pt As Point
    14.         Dim pt1 As Point
    15.         Dim cnt As Integer
    16.         Dim cnt1 As Integer
    17.         Dim lablsiz As Size = New Size(150, 20)
    18.         Dim txtsiz As Size = New Size(300, 20)
    19.         Dim i As Integer
    20.         Dim ds As DataSet
    21.         '--------------
    22.         Dim id As String
    23.         Dim obj As Object
    24.         Dim MyID As String
    25.         Dim ctr As Integer
    26.  
    27.         '-------------------
    28.         Try
    29.             If cn.State = ConnectionState.Closed Then
    30.                 cn.Open()
    31.             End If
    32.  
    33.             cn1.Open()
    34.            
    35.             str1 = "SELECT FldAn FROM tblCustomFieldAnswers WHERE AcctNum='" & m_stracctnum1 & "' And CustID = " & m_intcustnum1 & ""
    36.             cmd1 = New OleDbCommand(str1, cn1)
    37.             dr1 = cmd1.ExecuteReader
    38.  
    39.  
    40.             str = "SELECT FieldName FROM tblCustomFields WHERE AcctNum='" & m_stracctnum1 & "'And CustID = " & m_intcustnum1 & ""
    41.             cmd = New OleDbCommand(str, cn)
    42.             dr = cmd.ExecuteReader
    43.  
    44.            
    45.             If dr1.HasRows = False Then
    46.                 '-----------
    47.                 ctr = 1
    48.                 '------------
    49.                 While dr.Read
    50.                     pt1 = New Point(10, cnt1 + 10)
    51.                     labl = New Label
    52.                     labl.Text = dr(i) & ":"
    53.                     labl.TextAlign = ContentAlignment.MiddleRight
    54.                     labl.Location = pt1
    55.                     labl.Size = lablsiz
    56.                     Me.Controls.Add(labl)
    57.                     pt = New Point(175, cnt + 10)
    58.                     txtBox = New TextBox
    59.                     '--------------------------
    60.                     txtBox.Name = "txtboxno" & ctr
    61.                     '---------------------------------
    62.                     txtBox.Location = pt
    63.                     txtBox.Size = txtsiz
    64.      
    65.                     Me.Controls.Add(txtBox)
    66.                     ctr += 1
    67.                     cnt = cnt + (txtBox.Height + 15)
    68.                     cnt1 = cnt1 + (labl.Height + 15)
    69.                 End While
    70.  
    71.             Else
    72.                  '-----------
    73.                 ctr = 1
    74.                 '------------
    75.                 While dr.Read
    76.                     pt1 = New Point(10, cnt1 + 10)
    77.                     labl = New Label
    78.                     labl.Text = dr(i) & ":"
    79.                     labl.TextAlign = ContentAlignment.MiddleRight
    80.                     labl.Location = pt1
    81.                     labl.Size = lablsiz
    82.                     Me.Controls.Add(labl)
    83.                     While dr1.Read
    84.                         pt = New Point(175, cnt + 10)
    85.                         txtBox = New TextBox
    86.                     '--------------------------
    87.                     txtBox.Name = "txtboxno" & ctr
    88.                     '---------------------------------
    89.                         txtBox.Location = pt
    90.                         txtBox.Size = txtsiz
    91.  
    92.                         Me.Controls.Add(txtBox)
    93.                         txtBox.Text = dr1(i)
    94.                         ctr += 1
    95.                         cnt = cnt + (txtBox.Height + 15)
    96.  
    97.                     End While
    98.  
    99.                     cnt1 = cnt1 + (labl.Height + 15)
    100.                 End While
    101.             End If
    102.  
    103.         Catch ex As OleDbException
    104.  
    105.             MessageBox.Show(ex.ToString())
    106.  
    107.         End Try
    108.         If cn.State <> ConnectionState.Closed Then
    109.             cn.Close()
    110.         End If
    111.         If cn1.State <> ConnectionState.Closed Then
    112.             cn1.Close()
    113.         End If
    114.  
    115.     End Sub

    my thoughts:
    i can write a function/procedure to update the table and call it at the button click event
    since in the above code there can be anynumber of textboxes depending on the data in the table.so how can i check if the text has changed for each textbox and update it in the database?if you can give me ideas i would appreciate it.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: how to update data into the database from dynamically created controls

    Well, in that case, I would read the data from database to a dataset or datatable depending on what you need. Once you get your datasedt/datatable populated, you can always check the datatable for number of available rows/column and create your textboxes accordingly and use databind to bind the datatable to those textboxes. Now, when you want to update your database, you just need to check for the rowstate in the table and update only affected rows.
    This is just an idea, I've actually never done anything like (binding datatable to dynamically created textboxes) this before... So don't yell at me if it does not work
    Just currious though, why don't you use other control, like a datagrid to display the data? This would be much easier to handle in your situation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width