Validate csv input

G

Guest

Howdy

I'm trying to validate the content of a csv uploaded to a web page. I imported the csv into a DataSet and now I want to loop through all the elements in the DataSet to validate that each is of the appropriate type. After validation I will loop through the records again and write them to a SQL2000 table using a stored procedure.

My plan for validation is as follows. But I'm not entirely sure how to execute the validations. C# has wonderful built in validation for dropdownlists or textboxes and the like, but I need some help figuring out what to do now that the input does not come from a web control

Thanks for any help

--Terr

foreach (DataRow Rrow in ds.Tables["ImportCSV"].Rows

foreach(DataColumn Ccolumn in ds.Tables["ImportCSV"].Columns


if row item EmployeeNumber is not an integer
Halt loop, display error message
On row x, the EmployeeNumber "(value)" is not an integer
Please verify the EmployeeNumber on row x

if row item First_Name is not an alphanumeric string,
Halt loop, display error message
On row x, the First_Name "(value)" is not an alphanumeric string
Please verify the First_Name on row x

if row item First_Name has more than 30 characters,
Halt loop, display error message
On row x, the First_Name "(value)" has more than 30 characters
Please verify the First_Name on row x

... similar routine for the rest of the columns and rows in the DataSet


}
 
Joined
Oct 19, 2009
Messages
1
Reaction score
0
Use Flat File Checker Library

Hi,

add Flat File Checker library (FlatFileLibrary) to your project (can download source from flat-file.net website); then just create rules required for you validation through the Flat File Checker graphical user interface - to get Xml file that will contains validation rules.

You can use something something like this to address data errors in your files:

Code:
'-----------------------------------------------------------

' VB.Net Example:

Private WithEvents _files As FlatFileSchema

' Start validation
Sub RunValidation()
' Use Flat File Checker user interface to create Schema file.
 _files  = New FlatFileSchema(<Schema file path>) 
_files.RunChecks()
End Sub

' Feedback errors when file(s) validation is complete
Private Sub FileSetValidated(ByVal sender As Object, ByVal e As SchemaValidatedEventArgs) Handles _dataset.Validated
 Dim file As FlatFile
 Dim err As DataError
 For Each file In _files.Files
   For Each err In file.Errors
      ' Feed back error to users here
   Next err
 Next file

End Sub
'-----------------------------------------------------------

You can also add "on error" action to each data rule. Then only load lines without actions to your database. Or just reject file(s) if there are errors in this file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top