Results 1 to 5 of 5

Thread: [RESOLVED] Create and Fill New Column

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] Create and Fill New Column

    I need some helping filling a New Column with values

    I am using the following to strip out unwanted string in a string and i want to fill newly created column with split value. I dont know how to get the "Cityrev" into a column value

    Code:
     With dtlist
                Dim dtrow As DataRow
    
                For Each dtrow In dtlist.Rows()
                    Dim City As String = dtrow.Item("City").ToString()
                    Dim Cityrev() As String = City.Split(New [Char]() {"("c}, System.StringSplitOptions.None)
                    Dim revCity As String = Cityrev(Cityrev.Length - 2)
                Next
                .Columns.Add("Cityrev")
                For Each dtrow In dtlist.Rows
                    dtrow("Cityrev") = dtrow("City")
                Next dtrow
            End With

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

    Re: Create and Fill New Column

    There's not much point processing the data before you've got anywhere to put it. If you read what I posted in your other thread you'll see that I said to add the new column first. You can then loop through the rows and, for each row, get the data from the source column, process it and then put the result into the destination column. You've got all the pieces there. You just need to arrange them into the proper order.

    Whenever you write code, the code you write must implement the steps you should already know that your app needs to perform. If you try to write the code first, the likelihood that it will do what you want is low, because you don;t even know what you want yourself.
    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
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Create and Fill New Column

    Quote Originally Posted by jmcilhinney View Post
    There's not much point processing the data before you've got anywhere to put it. If you read what I posted in your other thread you'll see that I said to add the new column first. You can then loop through the rows and, for each row, get the data from the source column, process it and then put the result into the destination column. You've got all the pieces there. You just need to arrange them into the proper order.

    Whenever you write code, the code you write must implement the steps you should already know that your app needs to perform. If you try to write the code first, the likelihood that it will do what you want is low, because you don;t even know what you want yourself.
    Thanks I understand what your saying but getting the data from the source is what I am not able to see

    HTML Code:
     With dtlist
                .Columns.Add("Cityrev")
                Dim dtrow As DataRow
                For Each dtrow In dtlist.Rows()
                    Dim City As String = dtrow.Item("City").ToString()
                    Dim Cityrev() As String = City.Split(New [Char]() {"("c}, System.StringSplitOptions.None)
                    Dim revCity As String = Cityrev(Cityrev.Length - 2)
                    dtrow("Cityrev") = dtrow("City")
    
                Next dtrow
            End With

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

    Re: Create and Fill New Column

    Again, what did I say to do? Get the data from the original column, process it and put THE RESULT into the new column. What are you doing there? You are getting the data from the original column:
    Code:
    Dim City As String = dtrow.Item("City").ToString()
    You are processing the data:
    Code:
    Dim Cityrev() As String = City.Split(New [Char]() {"("c}, System.StringSplitOptions.None)
    Dim revCity As String = Cityrev(Cityrev.Length - 2)
    Finally you are putting THE ORIGINAL DATA into the new column:
    Code:
    dtrow("Cityrev") = dtrow("City")
    What's the point of processing the data if you're not going to use the result of that processing? Put the the result of the processing into the new column, NOT the original data.
    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: Create and Fill New Column

    Quote Originally Posted by jmcilhinney View Post
    Again, what did I say to do? Get the data from the original column, process it and put THE RESULT into the new column. What are you doing there? You are getting the data from the original column:
    Code:
    Dim City As String = dtrow.Item("City").ToString()
    You are processing the data:
    Code:
    Dim Cityrev() As String = City.Split(New [Char]() {"("c}, System.StringSplitOptions.None)
    Dim revCity As String = Cityrev(Cityrev.Length - 2)
    Finally you are putting THE ORIGINAL DATA into the new column:
    Code:
    dtrow("Cityrev") = dtrow("City")
    What's the point of processing the data if you're not going to use the result of that processing? Put the the result of the processing into the new column, NOT the original data.
    I understand that, my question is HOW do I put the result into the new column

    dtrow("Cityrev") = dtrow("revCity")

    gives me an error "revCity" does not belong to the datatable

    figured it out

    dtrow("Cityrev") = revCity
    Last edited by billboy; Nov 15th, 2010 at 12:00 AM. Reason: Solved

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