locked
validating a .csv file RRS feed

  • Question

  • User193749913 posted

    I am trying to upload a .csv file and then import it into a mysql database. Not all of the fields are required so I am getting an error if there is nothing in the first field. What am I needing to do to get around this? Below is some of my code so you can see what I am trying to do. Thanks in advance. 

    If fileName = "" Then
    
                LabelImport.Text = "You must upload a file first"
    
            Else
    
    
                Dim sMoveFile As String
    
                sMoveFile = Server.MapPath("CFRuploads/") & ifUpload.FileName
    
                Dim importFolder As String
                importFolder = Server.MapPath("CFRuploads/")
    
                Dim originalfilename As String
                originalfilename = Session("OriginalFileName")
    
                ' Connect to the Excel Spreadsheet
                Dim csvCon As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("CFRuploads/") & ";Extended Properties=""Text;HDR=No;FMT=Delimited;""")
    
                Dim command As OleDbCommand = New System.Data.OleDb.OleDbCommand("SELECT * FROM " & fileName & "", csvCon)
    
                Try
                    'Open the connection
                    csvCon.Open()
    
                    Dim adapter As OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(command)
    
    
                    Dim myTestReader As OleDbDataReader = command.ExecuteReader
    
                    Dim Counter As Integer = 0
    
                    Dim varErrorCount As Integer = 0
    
                    If myTestReader.HasRows Then
    
                        While myTestReader.Read And varErrorCount <= ErrorsDisplayed
    
                            varErrorFN = ""
    
                           ' Insert any required validations here...
                            
                               
                           If Not Regex.IsMatch(varFirstName, NRegex) Then
                                    varErrorFN = varErrorFN & "First Name "
                                    varGoodData = False
                           End If
                           
    
                           
                            If Not Regex.IsMatch(varLastName, NRegex) Then
                                    varErrorFN = varErrorFN & "Last Name "
                                    varGoodData = False
                            End If
                           
    
                            If Not Regex.IsMatch(varAddressNumber, ANRegex) Then
                                varErrorFN = varErrorFN & "Address Number "
                                varGoodData = False
    
                            End If
    
                            If Not Regex.IsMatch(varSubNumber, SNRegex) Then
                                varErrorFN = varErrorFN & "Sub Number "
                                varGoodData = False
    
                            End If
    
                            If Not Regex.IsMatch(varPrefixDirection, PDRegex) Then
                                varErrorFN = varErrorFN & "Prefix Direction "
                                varGoodData = False
    
                            End If
    
                            If Not Regex.IsMatch(varStreetName, StNRegex) Then
                                varErrorFN = varErrorFN & "Street Name "
                                varGoodData = False
    
                            End If
    
                            If Not Regex.IsMatch(varSuffixDirection, PDRegex) Then
                                varErrorFN = varErrorFN & "Suffix Direction "
                                varGoodData = False
    
                            End If
     
    Wednesday, July 16, 2008 12:45 PM

Answers

  • User-990694832 posted

    Try adding a StringIsEmptyOrNull check to your validations, for example:

                           If Not String.IsEmptyOrNull(varFirstName) AndAlso Not Regex.IsMatch(varFirstName, NRegex) Then
                                    varErrorFN = varErrorFN & "First Name "
                                    varGoodData = False
                           End If


    Hope this helps
    DK

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2008 3:41 PM