Results 1 to 4 of 4

Thread: Dynamically creating controls from a database value

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    London
    Posts
    44

    Dynamically creating controls from a database value

    I have an issue that I wonder if you can help me with. I am creatng a dynamic form generator, which generates a dynamic form at runtime, based on a database table.

    For eaxmple if a database field is a boolean value, then the form will add a check box. If the database field is a varchar (text field), the form will add a Textbox. If the database field is a foreign key, the form will add a combobox, and fill the dropdown list with all the values from the referenced column.

    The problem I have is that I want the user to be able to customise what control is added to which type of field via a database table. For example I might want a text field in the database, to map to a standard Textbox, or I might want a Text field to map to a Textbox from a different library, or an inherited control.

    In VB6 I could map an object from a variable using the CreateObject function.

    VB Code:
    1. Dim x As Object
    2.     Dim a As String
    3.    
    4.     a = "ADODB.Recordset"
    5.    
    6.     Set x = CreateObject(a)

    In .NET what I want to be able to do is something like this. I have a Table called FieldMappings with the following structure.

    ID FieldType xType ControlMapped
    1 Varchar 231 Textbox
    2 Bit 104 Checkbox

    From sysColumns I pull out all the Columns for a table called MyTable, and the mappings to a control as follows


    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'query to pull out all the fields for a table, and map them to a VB Object
    3.         Dim cmd As New SqlClient.SqlCommand("SELECT name, ControlMapped FROM sysColumns INNER JOIN ObjectMappings " & _
    4.                                             "ON sysColumns.xtype = FieldMappings.xtype WHERE sysColumns.id = 17435136", ExistingConnection)
    5.         Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
    6.  
    7.         Do While dr.Read
    8.             Select Case dr("ControlMapped")
    9.                 Case "CheckBox"
    10.                     Dim ctrl As New CheckBox
    11.                     ctrl.Name = dr("name")
    12.                     Me.Controls.Add(ctrl)
    13.                 Case "Textbox"
    14.                     Dim ctrl As New TextBox
    15.                     ctrl.Name = dr("name")
    16.                     Me.Controls.Add(ctrl)
    17.             End Select
    18.         Loop
    19.  
    20.         ExistingConnection.close()
    21.     End Sub


    But I don not want to use select case statements, because I do not want to hard code my mappings.

    What I actually want to be able to do is something like this.

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'query to pull out all the fields for a table, and map them to a VB Object
    3.         Dim cmd As New SqlClient.SqlCommand("SELECT name, ControlMapped FROM sysColumns INNER JOIN ObjectMappings " & _
    4.                                             "ON sysColumns.xtype = FieldMappings.xtype WHERE sysColumns.id = 17435136", ExistingConnection)
    5.         Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
    6.  
    7.         Do While dr.Read
    8.             'Use the name in the database to create the object without select case statements
    9.             Dim ctrl As Control
    10.             ctrl = CreateObject(dr(1))
    11.             ctrl.Name = dr("name")
    12.             Me.Controls.Add(ctrl)
    13.         Loop
    14.         dr.Close()
    15.  
    16.         ExistingConnection.close()
    17.     End Sub


    This way I can change the control that a database field is mapped to as and when I buy a new library, without having to re-write code.

    Is there a way of doing this?
    Last edited by royster; Feb 26th, 2004 at 05:58 AM.

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