none
How to save DataColumn.ExtendedProperties RRS feed

  • Question

  • I need help using DataColumn.ExtendedProperties.

    In a VisualBasic2008 WindowsForms project I have a DataSet with multiple related DataTables. The DataSet was created by the DataSet Wizard based off of an SQLCE database previously setup with the DataBase Explorer tool in Visual Studio.

    In one of the tables I would like to make available some additional information about some of the DataColumns. The DataColumn.ExtendedProperties looks like a good place to store this information. The information is static. I add the information from the Load event of the main form by checking the ExtendedProperties.Count property of the subject columns and then using the ExtendedProperties.Add() method to add the property if the count is 0. Code in the main form is then able to access the ExtendedProperties.

    I’m having two issues, which may be related.

    1. The ExtendedProperties are not persistent. That is, each time I run the application (I’ve only run it in the debugging environment) the main form finds the ExtendedProperties.Count to be 0 and adds the properties again. I suppose this isn’t an operational issue since the properties I’m adding are static in value so adding the same value each time the program starts doesn’t hurt anything. If the data wasn’t static I suppose I could create an item in My.Settings where it would be made persistent then use the setting value when adding the property. I’ve looked for DataColumn methods and ExtendedProperties methods that would save the ExtendedProperties but haven’t found them. I believe the DataSet definition is held in a project file, DSDataSet.Designer.vb, but I don’t know what would cause the added ExtendedProperties to be added to it. Since this Designer is auto-generated it doesn’t seem like something I should modify. The question is, can these DataColumn.ExtendedProperties be saved with the DataColumn properties so that they persist and if so, how.
    2. The ExtendedProperties are not available from other forms. The intent was to make the information in the ExtendedProperties available anywhere in the project that the DataColumn was being used. When I examine the ExtendedProperties.Count with code in a different form, it comes back 0. It seems as if I have two copies of the DataSet, one in the main form and one in the subsequent form. And, it seems that when the DataSet for the subsequent form came into existence it was built from the DataSet definition that did not have the ExtendedProperties set. As a work-around I can add code to set the ExtendedProperties in the Load event of any form that will make use of them. The question is, what am I doing (or not doing) that is keeping the ExtendedProperties from being available other than from the form in which they were added.

    Additional information.

    The main form and subsequent form design both include the DSDataSet and T1TableAdapter. The Load event for each of the forms uses the following statement to load the table with which I’m trying to use DataColumn.ExtendedProperties:

    Me.T1TableAdapter.Fill(Me.DSDataSet.T1)

     

     

     

    The TableAdapters were generated by VisualStudio as part of the DataSet.

    The ExtendedProperties are added in a procedure in a code module as follows:

    Public Sub CheckExtendedProperties(ByVal tDS As DSDataSet)
    If tDS.T1.Columns("ColumnName").ExtendedProperties.Count = 0 Then
    tDS.T1.Columns("ColumnName").ExtendedProperties.Add("KeyName", stringValue)
    End If
    End Sub

     

     

     

    This is called within the Load event of the form as follows:

    CheckExtendedProperties(DSDataSet)

     

     

    Any help would be appreciated on how to make use of this ExtendedProperties feature.

    Eric

    Saturday, March 6, 2010 6:44 PM

Answers

  • Hi Eric,

    From what you described, you want ExtendedProperties be added at design time rather than run time as your are currently doing. 
    You are right, you can't add the code in DataSet.designer.vb as that is generated code. However, you do can add your own code in a patial class. 

    Follow these steps and try it: 

    In my example, I have a dataset called DataSet1 and a table called T1 with a column called C1

    1. In the dataset designer, double click on T1's title bar. It will bring you to the parital class code editor like this: 

    Partial Class DataSet1
        Partial Class T1DataTable

            Private Sub T1DataTable_T1RowChanging(ByVal sender As System.Object, ByVal e As T1RowChangeEvent) Handles Me.T1RowChanging

            End Sub

        End Class
    End Class

    2. You can delete T1DataTable_T1RowChanging but add T1DataTable_Initialized to add an ExtendedProperty Key1 as below:

    Partial Class DataSet1
        Partial Class T1DataTable
            Private Sub T1DataTable_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
                C1Column.ExtendedProperties.Add("Key1", "test")
            End Sub
        End Class
    End Class

    3. Now if you write this code anywhere
            Dim ds As New DataSet1()
            If ds.T1.Columns("C1").ExtendedProperties.Count = 1 Then
                MsgBox("Got it")
            End If

    You will get it! 

    HTH, 

    John


     


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Eric-67220 Monday, March 8, 2010 2:32 PM
    Monday, March 8, 2010 7:23 AM

All replies

  • I was able to fix the second issue by simplifying things.

    Instead of adding DataSet, BindingSource and Table Adapters to the subsequent form, I added Public variables for DataSet and BindingSource in the subsequent form declarations section. Then, prior to using the subsequent form's .Show method from the main form, I set the subsequent form DataSet and BindingSource properties to the DataSet and BindingSource used in the main form. This way both forms are using the same DataSet, the DataSet’s tables and primary table BindingSource. This way the subsequent form doesn't need to add the ExtendedProperties, they are already there, added in the main form’s Load event.

    This still leaves question 1 which is how can the ExtendedProperties be saved as part of the design.

    Eric

    Saturday, March 6, 2010 11:38 PM
  • Hi Eric,

    From what you described, you want ExtendedProperties be added at design time rather than run time as your are currently doing. 
    You are right, you can't add the code in DataSet.designer.vb as that is generated code. However, you do can add your own code in a patial class. 

    Follow these steps and try it: 

    In my example, I have a dataset called DataSet1 and a table called T1 with a column called C1

    1. In the dataset designer, double click on T1's title bar. It will bring you to the parital class code editor like this: 

    Partial Class DataSet1
        Partial Class T1DataTable

            Private Sub T1DataTable_T1RowChanging(ByVal sender As System.Object, ByVal e As T1RowChangeEvent) Handles Me.T1RowChanging

            End Sub

        End Class
    End Class

    2. You can delete T1DataTable_T1RowChanging but add T1DataTable_Initialized to add an ExtendedProperty Key1 as below:

    Partial Class DataSet1
        Partial Class T1DataTable
            Private Sub T1DataTable_Initialized(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Initialized
                C1Column.ExtendedProperties.Add("Key1", "test")
            End Sub
        End Class
    End Class

    3. Now if you write this code anywhere
            Dim ds As New DataSet1()
            If ds.T1.Columns("C1").ExtendedProperties.Count = 1 Then
                MsgBox("Got it")
            End If

    You will get it! 

    HTH, 

    John


     


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    • Marked as answer by Eric-67220 Monday, March 8, 2010 2:32 PM
    Monday, March 8, 2010 7:23 AM
  • John, Thanks for the great idea. I’ve read a little about partials but haven’t yet made use of them (at least not explicitly). I’m anxious to try this out. I assume the added ExtendedProperty initialization will be present in the DataSet instantiated onto the form via the Form Designer at the design time and not just those declared in code using the New constructor. I notice TableName_Initialized() is not an event used in DataSet.Designer.vb nor is it available (at least as discovered via IntelliSense) in Form level code. What can I reference to discover the events available relative to a DataSet and it’s constituent parts within Partial Class code? Seeing this idea makes me wonder about what other DataSet ‘customizations’ may be possible. Please advise on the following two scenarios. 1) Mapping: Splitting a DateTime column in a DataBase to a Date and Time column in the DataSet. I’m doing this on the forms in several places in the application now but it would be easier if the DataSet presented two columns that were combined in the storage. 2) Conversion: When using a System DataType not supported in the DB some conversion must take place. For example, storing a System.Timestamp as Timestamp.Ticks in a 64-bit integer on the DB side. If the conversion could be handled in the Column definition within the DataSet class the DataSet could simply present a TimeStamp DataType even though it was being stored as a BigInt within the DB. If these seem practical, what procedures in the DataSet would I override with partials to effect these customizations? Thanks, Eric
    • Edited by Eric-67220 Monday, March 8, 2010 3:36 PM Formatting issues, had to remove formatting
    Monday, March 8, 2010 3:31 PM
  • Hi Eric,

    RE: I assume the added ExtendedProperty initialization will be present in the DataSet instantiated onto the form via the Form Designer at the design time and not just those declared in code using the New constructor
    Correct!

    For other question, it maybe better to open new threads to make the thread clear.

    Quick answer: Splitting a DateTime column in a DataBase to a Date and Time column in the DataSet
    It seems to me you should use expression column here.

    John
    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    Tuesday, March 9, 2010 4:44 AM
  • Thanks John,

    Hopefully still appropriate in this thread would be your input on what can be referenced to discover what events, properties and methods are available when writing partials for DataSets.  For instance, you used the event TableName_Initialized() and I'd like to know where to look to find out what other events, methods or properties can be used.  As I mentioned before IntelliSense doesn't show this or I'm not looking in the right place.

    I did post two additional threads on the Mapping and Conversion questions.

    Thanks,
    Eric

    Tuesday, March 9, 2010 4:59 PM
  • Hi Eric,

    A quick way for you to reference: If you use the VS editor, you can put the focus inside a class, and check the drop down list  in the top right corner of the code editor. you should see the list, that is one quick way.

    There should be other ways...

    John


    John Chen -- See my team blog: http://blogs.msdn.com/vsdata. All my posts are provided "AS IS" with no warranties, and confer no rights.
    Tuesday, March 9, 2010 5:42 PM