none
DataSet Names RRS feed

  • Question

  • I need the answer to a very SEEMINGLY easy question...

    I have some custom data sets and when I add them to my form I see two properties:

    'NAME' this is a name I give to the instance on my form (just like any other control) such as 'dataSet1'. (no duplicates allowed).

    "DataSetName" the name that is given to the custom data set.  (duplicates allowed).

    So if I have a DataSet with name dataSet1:

    (Name) = "pdsParent"

    DataSetName = 'pdsProcessFormDataSet'

    In code when I do: Trace.WriteLine(dataSet1.DataSetName);

    The output is "pdsProcessForm" which is NEITHER the (NAME) or DataSetName which is odd to me.

    That aside... how do I access the (NAME) property in code?  I can't seem to figure out how to do this.

    • Moved by Helen Zhou Friday, February 17, 2012 9:16 AM (From:Windows Forms General)
    Wednesday, February 15, 2012 7:30 PM

Answers

  • Correct me if I'm wrong, CommanderKeen, but it sounds to me like you want to know the name of the instance of the DataSet. But, a DataSet does not have a .Name property, like a TextBox or other controls have.

    One thing you could do, is create partial classes for all your DataSets (I'm assuming you're using Typed DataSets) to add additional functionality to them; in this case simply adding a Name property.

    // put these all in one file if you wish to
    public partial class pdsProcessFormDataSet
    {
        public string Name { get; set; }
    }
    public partial class MyOtherDataSet
    {
        public string Name { get; set; }
    }
    // etc.etc.etc. for all your DataSets

    Now, when you create instances of your DataSets, you'd set the Name property.

    this.pdsUnLinked = new pdsProcessFormDataSet();
    this.pdsUnlinked.Name = "pdsUnlinked";
    this.pdsLinked = new pdsProcessFormDataSet();
    this.pdsLinked.name = "pdsLinked";

    Will that do the trick for you?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, February 20, 2012 6:10 AM

All replies

  • Friend,
      dataset.DataSetName is not returning the objectName of the dataset. that is the value you can set while creating the dataset.

    Eg:

    DataSetds = newDataSet("MyDataSetName");

    The value you passing as the constructor argumen will get returned when you call ds.DataSetName property...


    -- Thanks Ajith R Nair

    Wednesday, February 15, 2012 7:53 PM
  • dataSet1 is a proper C# variable, while the DataSetName is a string that is primarily used for XML serialization.  See: http://msdn.microsoft.com/en-us/library/7se64052(v=vs.90).aspx.

    If no DataSetName is being set in the constructor, then DataSetName is NewDataSet.

       DataSet l_dataSet = new DataSet();
       MessageBox.Show(l_dataSet.DataSetName);
    

    If a DataSetName is being set through the constructor or through modification:

       DataSet l_dataSet = new DataSet("MyDataSetName");
       MessageBox.Show(l_dataSet.DataSetName);
    

    or

       DataSet l_dataSet = new DataSet();
       l_dataSet.DataSetName = "MyDataSet";
       MessageBox.Show(l_dataSet.DataSetName);
    

    Then the string that it will return will be whatever it was set to.  So, in short, you DataSetName is returning pdsProcessForm it is because that is what the name was set to somewhere in the code.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by horngsh Tuesday, February 21, 2012 12:25 AM
    Wednesday, February 15, 2012 8:49 PM
  • Thanks for the replies.

    How can I retrieve the NAME of the DataSet then?

    Like if I do.

    TextBox txtBox1 = new TextBox();

    Trace.WriteLine(txtBox1.Name);

    Will output "txtBox1";

    Is there no such thing for the DataSet?

    Wednesday, February 15, 2012 9:25 PM
  • I think I may need another example:

    In my designer I have TWO instances of ONE custom dataSets:

     private PASport.Proxy.StrongTypesNS.pdsProcessFormDataSet pdsLinked;
     private PASport.Proxy.StrongTypesNS.pdsProcessFormDataSet pdsUnLinked;

    When I put these in the designer the code is written (just how you both describe):

    this.pdsLinked.DataSetName = "pdsProcessFormDataSet";
    this.pdsUnLinked.DataSetName = "pdsProcessFormDataSet";

    However, when I look in my designer the data sets both have different names (obviously).

    For my circumstance this poses a problem.  I'd like to get the names 'pdsLinked' and 'pdsUnlinked'.

    Wednesday, February 15, 2012 9:33 PM
  • There are some convoluted ways to do it, but the most straight forward would be to re-generate the DataSets with the DataSetNames that you desire.  Another way, considering the limited number of DataSets that you are dealing with, would be to create a function that will return the names that you wish.  Something like:

      private string GetDataSetName(DataSet dataSetToTest)
      {
       if (dataSetToTest == pdsLinked)
        return "pdsLinked";
    
       if (dataSetToTest == pdsUnlinked)
        return "pdsUnlinked";
    
       return "";
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Wednesday, February 15, 2012 10:15 PM
  • hmmm thanks for the idea... the problem with that is that I'm actually collecting the datasets from OUTSIDE the form... therefore I don't actually know what dataset I have.  This is one reason why I actually want the name of the dataset.

    I have about 180 forms, each contains a unique number of datasets of different types.  When a form is instantiated another object from outside can look at the form and collect all the datasets.  From this object though... we don't have the names.  =[

    Thursday, February 16, 2012 6:43 PM
  • Then you probably don't want to be going after localized variable names, even at the class scope.  DataSetName is likely what you want, as it will transit with the DataSet from the serialization, so perhaps you should be looking at ways of coercing your DataSet names into something that you are willing to deal with.  With that many already created, I don't envy you.

    Measure twice, cut once ;)


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Thursday, February 16, 2012 7:05 PM
  • Hi CommanderKeen,

    This question is more related to  ADO.NET DataSet issues, I'm moving it there to make it get better support. Thank you for your understanding.

    Sincerely,

    Helen Zhou [MSFT]
    MSDN Community Support | Feedback to us

    Friday, February 17, 2012 9:16 AM
  • Hello CommanderKeen, 

    There are datasets and strongly typed datasets, which use as base class the DataSet class. 

    The strongly typed datasets are created in an XSD and you can find them in your Solution explorer and view the designer files of those (either vb or C#)

    So there are not even 2 names but many names which are generated by the generator (wizard whatever you name them)

    The DataSetName is the property which is used for merging datasets (AFAIK the only place but there will be probably more). 

    The same like the Columnname and TableName, a kind of left over from framework 1.0 but still because of not getting breaking changes probably kept the same.

    Be aware that the strongly typed names are Class names, while the content of a DataSetName property is a part of an instance (object)


    Success
    Cor




    • Edited by Cor Ligthert Saturday, February 18, 2012 9:45 AM
    Saturday, February 18, 2012 9:44 AM
  • Correct me if I'm wrong, CommanderKeen, but it sounds to me like you want to know the name of the instance of the DataSet. But, a DataSet does not have a .Name property, like a TextBox or other controls have.

    One thing you could do, is create partial classes for all your DataSets (I'm assuming you're using Typed DataSets) to add additional functionality to them; in this case simply adding a Name property.

    // put these all in one file if you wish to
    public partial class pdsProcessFormDataSet
    {
        public string Name { get; set; }
    }
    public partial class MyOtherDataSet
    {
        public string Name { get; set; }
    }
    // etc.etc.etc. for all your DataSets

    Now, when you create instances of your DataSets, you'd set the Name property.

    this.pdsUnLinked = new pdsProcessFormDataSet();
    this.pdsUnlinked.Name = "pdsUnlinked";
    this.pdsLinked = new pdsProcessFormDataSet();
    this.pdsLinked.name = "pdsLinked";

    Will that do the trick for you?


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, February 20, 2012 6:10 AM
  • Bonnie,

    Excuse me for my ignorance, but does that not create an obsolete property which would be the same as the property DataSetName?

    In that case I would prefer a non automatic property which simply uses the DataSetName as field to set and get. 

    However, than it is even more obsolete and only confusing for those who use this class.


    Success
    Cor


    Monday, February 20, 2012 8:12 AM
  • Hi Cor,

    I don't believe that's what CommanderKeen is looking for. Let's put together excerpts from a few quotes he posted:

    How can I retrieve the NAME of the DataSet then?

    Like if I do.

    TextBox txtBox1 = new TextBox();

    Trace.WriteLine(txtBox1.Name);

    Will output "txtBox1";

    For my circumstance this poses a problem.  I'd like to get the names 'pdsLinked' and 'pdsUnlinked'.

    So, obviously, he doesn't want the DataSetName of his instances, they would be the same name for two instances. Look at it this way. txtBox1.Name isn't "TextBox", it's "txtBox1". Likewise, he doesn't want pdsLinked.Name to be "pdsProcessFormDataSet",  or whatever the DataSetName happens to be. He wants pdsLinked.Name to show as "pdsLinked", just as the .Name property of the txtBox1 TextBox does.

    That's what my suggestion accomplishes. If my assumption about what he wants is wrong, then my suggestion is wrong. Let's see what CommanderKeen has to say ....


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, February 20, 2012 3:02 PM
  • Bonnie,

    I don't discuss how to get it done, nice way you did it, I discuss if it is a good way of developing to have two properties for the same goal.

    Let us say in this way a propety for the name your father has called you and the way your mother has called you, but not making that clear in the property names.

    :-)

    Therefore I wrote obsolete (and then even before it was ever used).

    :-)


    Success
    Cor

    Tuesday, February 21, 2012 10:08 AM