Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:21:10+00:00 2026-06-11T22:21:10+00:00

I have some code which reads every row of a CSV file and if

  • 0

I have some code which reads every row of a CSV file and if the value doesn’t match the correct value, it will add it to the error list which is returned to the users screen. The problem I am having is with the regex itself.

protected void ReadData(string filePath, bool upload)
{
    StringBuilder sb = new StringBuilder();
    #region upload
    if (upload == true) // CSV file upload chosen
    {
        using (CsvReader csv = new CsvReader(new StreamReader(filePath), true)) // Cache CSV file to memory
        {
            int fieldCount = csv.FieldCount; // Total number of fields per row
            string[] headers = csv.GetFieldHeaders(); // Correct CSV headers stored in array 
            SortedList<int, string> errorList = new SortedList<int, string>(); // This list will contain error values
            bool errorFlag = false;
            int errorCount = 0;

            // Check if headers are correct first before reading data
            if (headers[0] != "first name" || headers[1] != "last name" || headers[2] != "job title" || headers[3] != "email address" || headers[4] != "telephone number" || headers[5] != "company" || headers[6] != "research manager" || headers[7] != "user card number")
            {
                sb.Append("Headers are incorrect");
            }

            else 
            {
                while (csv.ReadNextRecord())
                    try
                    {
                        //Check csv obj data for valid values
                        for (int i = 0; i < fieldCount; i++)
                        {
                            if (i == 0 || i == 1) // FirstName and LastName
                            {
                                if (Regex.IsMatch(csv[i].ToString(), "[a-zA-Z]", RegexOptions.IgnoreCase)) //REGEX letters only min of 5 char max of 20
                                {
                                    errorList.Add(errorCount, csv[i]);
                                    errorCount += 1;
                                    errorFlag = true;
                                    string text = csv[i].ToString();
                                }
                            }

                            else if (i == 5) // Company name
                            {
                                string text = csv[i];
                                text.Replace("&", "and");
                            }
                        } 

                        if (errorFlag == true)
                        {
                            sb.Append("<b>" + "Number of Error: " + errorCount + "</b>");
                            sb.Append("<ul>");
                            foreach (KeyValuePair<int, string> key in errorList)
                            {
                                sb.Append("<li>" + key.Value + "</li>");
                            }
                        }
                        else // All validation checks equaled to false. Create User
                        {

                            ORCLdap.CreateUserAccount(rootLDAPPath, svcUsername, svcPassword, csv[0], csv[1], csv[2], csv[3], csv[4], csv[5], csv[7]);
                            sb.Append("<b>New user data uploaded successfully</b>");
                        }

                    }// end of try

                    catch (Exception ex)
                    {
                        sb.Append(ex.ToString());
                    }

                    finally
                    {
                        lblMessage.Text = sb.ToString();
                        sb.Remove(0, sb.Length);

            }
        }
    }
    #endregion

The lblMessage.text contains this html:

Number of Error: 4

  • David1212
  • smith
  • Nick444
  • Gowdy333

When it should be 3 errors because smith doesnt contain a number.

Does anyone have suggestions for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:21:12+00:00Added an answer on June 11, 2026 at 10:21 pm

    You need to add word boundaries to your regex, or starting ‘^’ and end ‘$’
    i.e.
    ^[a-zA-Z]+$

    http://regexr.com?3298g

    Your current regex is incorrect, and will match any string which contains a-z or A-Z , any letter,at any position.

    http://regexr.com?3298j

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some python code which runs every 10 minutes or so. It reads
I have a function(please see code below) which reads some data from the web.
I have some code which takes strings representing hexadecimal numbers - hex colors, actually
I have some code which I want to document with in-body comments like so:
I have some code which I'm currently using to change the static-IP of a
I have some code which populates a hashtable with a question as the key
I have some code which handles data files and reports an error when it
We have some code which sorts a list of addresses based on the distance
I have some code which is built both on Windows and Linux. Linux at
I have some code which builds an array of date ranges. I then call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.