Results 1 to 4 of 4

Thread: Dynamically creating controls from a database value

  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.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I have been involved in a project like this, where we created controls based on a database value ( and assigned their labels, and positioning based on those values -> end result being a completely customizable app at the change of database field).

    The code I used was involved reflection. I believe it was Activator.CreateInstanceOf. You pass the library name and control name (as text), among other things to this method. As long as your project has references to the dll's, this will work. (if you add a new control library, you simply have to update your app's references)

    I would give you some code, unfortunately, that project is locked up in a vaulted server at the moment.
    Last edited by nemaroller; Feb 27th, 2004 at 08:25 AM.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    London
    Posts
    44

    Just found that out too....

    Thanks for the reply

    Yes I did manage to get hold of a book, and found this method as the way to do this. The good news is that I do not need to re-write code, but I will need to update references. The fact that I can create the references at runtime, means that I should be able to store those references on a databse too. This means I should, in theory, not need to recomplile code at all.

  4. #4
    Addicted Member
    Join Date
    Mar 2001
    Location
    The Netherlands
    Posts
    184

    Re: Dynamically creating controls from a database value

    hi,

    well i have been looking at this also. I was wondering if you solved it (and how?)

    Regards,

    per-i

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