locked
length cannot be less than zero. parameter name RRS feed

  • Question

  • User667536117 posted
    Protected Sub btnUploadNewDocument_Click(sender As Object, e As EventArgs) Handles btnUploadNewDocument.Click
            Try
                ToggleErrorSummary(False)
    
                If Not NewDocumentUpload.HasFile Then
                    Page.Validators.Add(New DummyValidator(Messages.NewDocumentUploadEmpty, contractorValGroup))
                    ToggleErrorSummary(True)
                    Exit Sub
                End If
    
                Dim intDocumentTypeSeq = CInt(cboDocumentType.SelectedValue)
                Dim documentType As DocumentType = DocumentType.GetByDocumentTypeSeq(intDocumentTypeSeq)
                Dim parameterDocumentPath As Parameter = Parameter.GetByDescription(Parameter.ParameterDescription.DocumentPath, PlantId)
    
                
                Dim folder = New DirectoryInfo(parameterDocumentPath.Value)
                Dim fileInfos() As FileInfo = folder.GetFiles(documentType.ShortDescription & "*")
                Dim intFileCounter = 1
    
                For Each fi As FileInfo In fileInfos
                    Dim intNumberStartPos = CShort(fi.Name.IndexOf("_") + 1)
                    Dim intNumberEndPos = CShort(fi.Name.IndexOf("."))
                    Dim intLength = intNumberEndPos - intNumberStartPos
                    Dim intCounter = CShort(fi.Name.Substring(intNumberStartPos, intLength))
    
                    If intCounter >= intFileCounter Then
                        intFileCounter = intCounter
                        intFileCounter = CShort(intFileCounter + 1)
                    End If
                Next
    
                Dim documentTypeDesc = documentType.Description
                Dim validFrom, validTill As Date?
    
                If Not String.IsNullOrWhiteSpace(txtValidFromNew.Text) Then
                    validFrom = DateTime.ParseExact(txtValidFromNew.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
                End If
    
                If Not String.IsNullOrWhiteSpace(txtValidTillNew.Text) Then
                    validTill = DateTime.ParseExact(txtValidTillNew.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture)
                End If
    
                If Not validFrom.HasValue OrElse Not validTill.HasValue Then
                    Page.Validators.Add(New DummyValidator(String.Format(Messages.ValidityDatesMustBeFilledInForDocumentType, documentTypeDesc), contractorValGroup))
                    ToggleInvalidFields(New List(Of String) From {txtValidFromNew.ClientID, txtValidTillNew.ClientID}, ".upload-control")
                    ToggleErrorSummary(True)
                    Exit Sub
                End If
    
               
                Dim fileToUpload = New FileInfo(NewDocumentUpload.FileName)
                Dim fileName As String = $"{documentType.ShortDescription}_{intFileCounter.ToString}{fileToUpload.Extension}"
                Dim filePath As String = Path.Combine(folder.FullName, fileName)
                NewDocumentUpload.SaveAs(filePath)
    
                
                Dim newDocument As New Document() With {
                .IsNew = True,
                .DocumentSeq = Data.CardHolder.ListOfDocuments.Count + 1,
                .DocumentTypeSeq = intDocumentTypeSeq,
                .DocumentTypeDescription = cboDocumentType.SelectedItem.Text,
                .DocumentName = filePath,
                .ValidFrom = validFrom,
                .ValidTill = validTill
            }
    
                Data.CardHolder.ListOfDocuments.Add(newDocument)
    
               
                UpdateDocumentsGrid()
    
               
                ToggleUploadButtonEnabled(Data.CardHolder.ListOfDocuments.Count < 1)
    
               
                EnforceLimosaDatesToAreaAccess()
            Catch ex As Exception
                Dim Logger As New Vcg.Common.Logging.Logging(Tools.ApplicationLog, "Document Upload")
            Logger.WriteToLog(300, "Documents Realted error", ex, Now)
            End Try
        End Sub

    I am getting error on test server not in local machine 

    Thanks in advance !

    Friday, June 12, 2020 7:04 PM

All replies

  • User409696431 posted

    What line is giving you the error?

    Saturday, June 13, 2020 9:22 PM
  • User288213138 posted

    Hi Nidhi Rajput,

    I am getting error on test server not in local machine 

    Can your code work normally on the local machine? Did you deploy your project to iis?

    length cannot be less than zero. parameter name

    Dim intNumberStartPos = CShort(fi.Name.IndexOf("_") + 1)
                    Dim intNumberEndPos = CShort(fi.Name.IndexOf("."))
                    Dim intLength = intNumberEndPos - intNumberStartPos
                    Dim intCounter = CShort(fi.Name.Substring(intNumberStartPos, intLength))

    Usually, this error means that you provide a less than zero length to the Substring method. So please debug your code to see if there is a problem with the parameters in the Substring method.

    If you still can't solve the problem, please post more detailed information about your question.

    Best regards,

    Sam

    Monday, June 15, 2020 3:09 AM
  • User667536117 posted

    Hi,

    In my local machine i am not getting any error .How can i reproduce this error It is working fine But after depoly the code in test it throwing error on  

    Tuesday, June 16, 2020 6:25 AM
  • User667536117 posted

    Hi Samwu,

    In my local i am not getting this error 

    thanks & regards 

    Tuesday, June 16, 2020 6:26 AM
  • User288213138 posted

    Hi Nidhi Rajput,

    In my local machine i am not getting any error .How can i reproduce this error It is working fine But after depoly the code in test it throwing error on  

    Did you deploy the project to iis? if so, you can try to dig the exception. First of all , please check your event viewer.  If no exception can be found in event viewer. It might be a first chance exception. Then you need to collect dump file with debug diagnostic collection and analyze the dump file with WINDBG.

    Debug Diagnostic Tool

    Using Microsoft's Debug Diagnostics Tool

    Best regartds,

    Sam

    Tuesday, June 16, 2020 8:51 AM
  • User667536117 posted

    Hi Samwu

    In this below line showing error .

    For Each fi As FileInfo In fileInfos

    thanks & regards 

    Nidhi

    Tuesday, June 16, 2020 10:41 AM
  • User753101303 posted

    Hi,

    Likely not really on this line. As told already it happens often in string methods such as Substring when providing mistakenly a negative value. The call stack should tell where it happens (andt the message doesn't give the actual parameter name ???).

    Make sure all files have a _ character coming before the first . character. For example if you have a file such as MyFile.Test_something.txt your code won't work.

    I would use build in methods such as https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.8 (it likely keeps all until the LAST . character).

    Edit : having something that works on localhost but not on a production server is quite common and is basically irrelevant. Here more likely local files are matching your assumption while this is not the case for files found on the production machine.

    Tuesday, June 16, 2020 11:14 AM
  • User475983607 posted

    Often, this error is a misunderstanding where the code tries to access files on the local machine.  The code works as expected on the development machine because the development machine is both the client and the server.  Once deployed to a remote machine the code no longer functions because the files are missing on the remote machine.

    Tuesday, June 16, 2020 11:16 AM