Hi All ,
how to validate the data in csv Import in asp.net. For example ,if i integer value instead of string ,import should not happened.
If anyone knows. Guide me with example code
Hi All ,
how to validate the data in csv Import in asp.net. For example ,if i integer value instead of string ,import should not happened.
If anyone knows. Guide me with example code
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mohamed Azarudeen ZPosted Jun 26, 2023, 7:56 AM
Hi karthik
Read the CSV file: Start by reading the CSV file and extracting the data from it. There are several libraries available for parsing CSV files in ASP.NET, such as CsvHelper, FileHelpers, or custom implementations.
Define a data model: Create a data model that represents the structure of the CSV data. Each property in the model should correspond to a column in the CSV file. Ensure that the data types of the properties match the expected types in the CSV file.
Implement data validation: Perform data validation on each row of the CSV file as you read it. You can use the data model's attributes or custom validation logic to validate the data. For example, you can use the
Requiredattribute to ensure a value is present, or custom validation methods to validate specific data conditions.Here's an example code snippet demonstrating how you can perform data validation during CSV import:
Brahma Prakash ShuklaPosted Jun 27, 2023, 8:16 AM
To validate the data in a CSV import in ASP.NET and ensure that only valid values are imported, you can follow these steps:
In this example, the
Nameproperty is marked as required, and theAgeproperty is validated to be a non-negative integer.CsvHelperorTextFieldParserto read the CSV file. Here's an example usingCsvHelper:In this example, the
ImportCsvmethod reads the CSV file usingCsvHelper, iterates through each record, and calls theValidateCsvRecordmethod to perform additional custom validation along with the data annotation validation. The valid records are added to thevalidRecordslist for further processing.In this example, the
ImportCsvendpoint receives the CSV file as anIFormFileparameter. It checks if a file was uploaded and saves it to a temporary file. It then uses theCsvImportServiceto import and validate the CSV data.By combining these steps, you can read and validate the data in the CSV file during the import process and ensure that only valid values are imported. Feel free to adapt the code to fit your specific requirements and business logic.
Karthik KPosted Jun 27, 2023, 1:56 AM
@Mr Azhur, Thanks for your response. I have a query on your solution . In Your code sample public List ImportAndValidateCsvData(Stream csvStream) .
what value need to pass in this csvStream.
In my Case , string csvData = File.ReadAllText(FilePath); we are getting the csv data inthis way ,if i want to follow the code how to pass the csv file . do i need to pass in the csvstream place.kindly revert me .
Thanking you,
Karthik.K