none
Argument 'Length' must be greater or equal to zero. RRS feed

  • Question

  • If Len(Row.Area) > 0 Then
                Row.pCommunity = Right(Row.Area, Len(Row.Area) - 4)
            End If

    When i run this in the ssis package within script task i am getting this error.

    We are scarpping area like 

    7 - Clear Lake Area to clear lake area

    Argument 'Length' must be greater or equal to zero.


    • Edited by SQL Begin Wednesday, September 27, 2017 10:01 PM
    Wednesday, September 27, 2017 9:50 PM

All replies

  •  That would suggest that the part in bold below is coming up with a negative number which is less than zero.

    Row.pCommunity = Right(Row.Area, Len(Row.Area) - 4)

     Not sure what it is that you are doing or what "Row.Area" is but,  if you are going to subtract 4 from it,  you need to make sure Row.Area is at least 4 to begin with....

        If Len(Row.Area) > 3 Then
             Row.pCommunity = Right(Row.Area, Len(Row.Area) - 4)
        End If


    If you say it can`t be done then i`ll try it

    Wednesday, September 27, 2017 10:05 PM
  • i am trying to remove the number from area field 

    example

    7 - Clear Lake Area to clear lake area

    i am trying to write if statement 

    If Len(Row.Area) > 2 Then
                Row.pCommunity = Right(Row.Area, Len(Row.Area) - 4)
            ElseIf Len(Row.Area) <= 2 Then
                Row.pCommunity = Row.Area
            Else
                Row.pCommunity_IsNull = True
            End If

    but this this only giving the area value with lenght 2 and less than two but i am planning to get all the rows in area. 


    • Edited by SQL Begin Wednesday, September 27, 2017 10:13 PM
    Wednesday, September 27, 2017 10:10 PM
  •  Like i said,  i am not sure what "Row.Area" is but,  if all you want to do is remove the number then perhaps try this...

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = "7 - Clear Lake Area"
            Dim indx As Integer = s.IndexOf("-")
            If indx > -1 Then
                s = s.Substring(indx + 2)
            End If
            Label1.Text = s 'shows "Clear Lake Area" in the label
        End Sub
     

     

     Edit:  Actually,  it would be safer to do it like this....


        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As String = "7 - Clear Lake Area"
            Dim indx As Integer = s.IndexOf("- ")
            If indx > -1 AndAlso s.Length >= indx + 2 Then
                s = s.Substring(indx + 2)
            End If
            Label1.Text = s 'shows "Clear Lake Area" in the label
        End Sub




    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, September 27, 2017 10:44 PM
    Wednesday, September 27, 2017 10:25 PM
  • area field is basically community field

    values for area field

    33 - Galveston County
    7 - Clear Lake Area
    82 - Other - International
    10 - Cy-Fair
    32 - Far Northeast
    9 - Central North
    33 - Galveston County
    14 - Far Northwest
    22 - Central West
    16 - Central
    12 - North
    13 - Northwest
    42 - Trinity County
    15 - Montgomery County SW
    44 - Polk County
    13 - Northwest
    24 - Spring Branch
    1 - Northeast
    40

    2

    so i am trying to achieve here is remove the number aand hyphen where possible and map , if the area has just number values map number value only.

    Thank you for your help in advance

    this is what i had before and it was working fine until today.

    If Len(Row.Area) > 0 Then
                Row.pCommunity = Right(Row.Area, Len(Row.Area) - 4)
            End If
    

    • Edited by SQL Begin Wednesday, September 27, 2017 10:37 PM
    Wednesday, September 27, 2017 10:35 PM
  • area field is basically community field

    You need to know the actual values that you are dealing with.

    Insert a breakpoint at that line.  When the code stops there, examine the variable Row.Area.  Is the length at least 4?  If not, you need to backtrack through the code to see where that value is getting set, and why it isn't what you expect it to be (or what it has been until today). 

    Wednesday, September 27, 2017 10:47 PM
  • You could use a regular expression to do your string checking and parsing

            Dim exp As New System.Text.RegularExpressions.Regex("\d - (.*)")
            For Each Row In rows
                Dim match = exp.Match(Row.Area)
                If match.Success Then
                    Row.pCommunity = match.Groups(1).Value
                ElseIf Row.Area.Length = 0 Then
                    Row.pCommunity_IsNull = True
                Else
                    Row.pCommunity = Row.Area
                End If
            Next

    Wednesday, September 27, 2017 11:00 PM
  • If you want to remove the digits and hyphen.

    Dim CommunityList As New List(Of String) From
        {
            "33 - Galveston County",
            "7 - Clear Lake Area",
            "82 - Other - International",
            "1 - Northeast",
            Nothing,
            "40",
            "10 - Cy-Fair"
        }
    
    For index As Integer = 0 To CommunityList.Count - 1
        If Not String.IsNullOrWhiteSpace(CommunityList(index)) Then
            Dim s As String = New String(CommunityList(index).TakeWhile(Function(c) (Not Char.IsLetter(c))).ToArray())
            CommunityList(index) = CommunityList(index).Replace(s, "")
        End If
    Next
    
    CommunityList.ForEach(Sub(item) Console.WriteLine($"'{item}'"))

    If no text, just a number 

    Dim CommunityList As New List(Of String) From
        {
            "33 - Galveston County",
            "7 - Clear Lake Area",
            "82 - Other - International",
            "1 - Northeast",
            Nothing,
            "40",
            "10 - Cy-Fair"
        }
    
    
    For index As Integer = 0 To CommunityList.Count - 1
        If Not String.IsNullOrWhiteSpace(CommunityList(index)) Then
            If Not Integer.TryParse(CommunityList(index), Nothing) Then
                Dim s As String = New String(CommunityList(index).TakeWhile(Function(c) (Not Char.IsLetter(c))).ToArray())
                CommunityList(index) = CommunityList(index).Replace(s, "")
            End If
        End If
    
    Next
    
    CommunityList.ForEach(Sub(item) Console.WriteLine($"'{item}'"))


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, September 28, 2017 12:05 AM
    Moderator
  • Hi SQL,

    Can you help us to confirm where do you store these values at the beginning? ListBox, TextBox, or other? I do one simple sample that you can take a look, these value is stored into the ListBox.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each item In ListBox1.Items
                Dim number As Integer
                If item.ToString().Trim() = Nothing Then
                    ListBox2.Items.Add("")
                ElseIf Integer.TryParse(item.ToString().Trim(), number) Then
                    ListBox2.Items.Add(item.ToString().Trim())
                Else
                    Dim array() As String = item.ToString().Split("-")
                    ListBox2.Items.Add(array(1).Trim())
    
                End If
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Monday, October 9, 2017 8:51 AM
    Thursday, September 28, 2017 4:11 AM
    Moderator
  • Check the next example, which is based on Regular Expressions too:

       Dim example = "9 - Central North"

       Dim result = Regex.Replace(example, "^\d+\s*-\s*(?=.)", String.Empty)

    If the string contains just a number, then the number will be kept.

    Thursday, September 28, 2017 5:58 AM