Results 1 to 3 of 3

Thread: [2005] column datatype for built-in-memory-datatable problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    [2005] column datatype for built-in-memory-datatable problem

    I need to build a new table in memory that has two columns. Each will contain a from a different database (well one's a read in XML dataset) It's basically a table to translate the former primary key to the new primary key while the XML file gets imported into the Access database. So I need the datatype on both columns to be string. If I just make it and add data like this:

    Code:
                Dim temptable As New DataTable
                temptable.Columns.Add("one")
                temptable.Columns.Add("two")
                    Dim temp(1) As String
                    temp(0) = "fish"
                    temp(1) = "cow"
                   temptable.rows.add(temp)
    what will it do? Will it convert fish and cow to some wacky general object datatype and append stuff to it or put the value literally in double quotes or will it automatically change the column datatype to string or will it just crash saying I haven't specified a datatype yet or something?
    By the way, I'm adding columns with the simplest overload because I can't figure out how the heck to write the second part for the one where it's column name then datatype. You'd think it would be System.Data.DataColumn.DataType.string like the popup window suggests but nope
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] column datatype for built-in-memory-datatable problem

    Quote Originally Posted by Desolator144
    By the way, I'm adding columns with the simplest overload because I can't figure out how the heck to write the second part for the one where it's column name then datatype. You'd think it would be System.Data.DataColumn.DataType.string like the popup window suggests but nope
    I'm sorry, what? You can't work out how to specify the type when the documentation provides this code example:
    Quote Originally Posted by MSDN
    vb.net Code:
    1. Private Sub AddColumn()
    2.     Dim columns As DataColumnCollection = _
    3.         DataSet1.Tables("Orders").Columns
    4.  
    5.     ' Add a new column and return it.
    6.     Dim column As DataColumn = columns.Add( _
    7.         "Total", System.Type.GetType("System.Decimal"))
    8.     column.ReadOnly = True
    9.     column.Unique = False
    10. End Sub
    I can only assume that that means that you didn't actually bother to read the documentation.

    That said, there's a simpler way still. That code could be rewritten like this:
    vb.net Code:
    1. Private Sub AddColumn()
    2.     Dim columns As DataColumnCollection = _
    3.         DataSet1.Tables("Orders").Columns
    4.  
    5.     ' Add a new column and return it.
    6.     Dim column As DataColumn = columns.Add( _
    7.         "Total", GetType(Decimal))
    8.     column.ReadOnly = True
    9.     column.Unique = False
    10. End Sub
    GetType is a VB.NET operator that gets a System.Type object from a data type. A Type object is an instance of the Type class that represents a data type. In that code the data type is Decimal and GetType returns a Type object that represents that type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2005
    Posts
    257

    Re: [2005] column datatype for built-in-memory-datatable problem

    well first of all, that's the worst method name ever. In fact, that's the dumbest method ever. The way they named it, you "get the type" of a system type and it returns a system type. If I didn't know better I'd say the method actually does nothing lol. But thanks for the answer
    And btw I've found a useful answer in the MSDN library like 1% of the time so I go there as a last resort. Once in a great while I'll check it for something really general that I know is in there or a good description of a method or something.
    I tried to end process on Visual Studio 2005
    but PETA stopped me saying it's smart enough
    to be a living creature

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