Click here to Skip to main content
15,845,742 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm working with uploading a CSV file but one of the data fields in the CSV file maybe marked with a * at the end i.e. SI 9 MS*, I want to remove this * char before uploading this data to the DB. How do I do this? I'm reading my CSV file like this:
C#
//Read in contents of file into database
using (CsvReader reader = new CsvReader(strFolderPath + @"\" + strFileName))
{
  int intCounter = 0;

  foreach (string[] values in reader.RowEnumerator)
  {
     //NOTE: First 4 rows are header information
     if (reader.RowIndex > 4)
     {
        if (DAL.InsertPriceList(RegionID, values, Session["Username"].ToString()))
        {
           //throw new Exception("Row " + (intCounter+1).ToString() + " - " + DAL.DBErrorMsg);
           if (DAL.DBErrorMsg != "")
           {
              StringBuilder errormessage = new StringBuilder();
              errormessage.Append(DAL.DBErrorMsg);                                                
           }
        }
        intCounter++;
     }
}

lblUploadSuccess.Text = string.Format("{0} Price List items uploaded to database", intCounter);
lblUploadSuccess.Visible = true;
PopulatePriceListGridView();
}
Posted

1 solution

Try this:
C#
//Read in contents of file into database
using (CsvReader reader = new CsvReader(strFolderPath + @"\" + strFileName))
{
  int intCounter = 0;

  foreach (string[] values in reader.RowEnumerator)
  {
     //Removing * from values
     string[] newVal = new string[values.Length];
     int count = 0;
     foreach (string val in values)
     {
        newVal[count] = val.Replace("*", "");
        count++;
     }
     //NOTE: First 4 rows are header information
     if (reader.RowIndex > 4)
     {
        if (DAL.InsertPriceList(RegionID, newVal, Session["Username"].ToString()))
        {
           //throw new Exception("Row " + (intCounter+1).ToString() + " - " + DAL.DBErrorMsg);
           if (DAL.DBErrorMsg != "")
           {
              StringBuilder errormessage = new StringBuilder();
              errormessage.Append(DAL.DBErrorMsg);
           }
        }
        intCounter++;
     }
}

lblUploadSuccess.Text = string.Format("{0} Price List items uploaded to database", intCounter);
lblUploadSuccess.Visible = true;
PopulatePriceListGridView();
}




--Amit
 
Share this answer
 
v2
Comments
pmcm 12-Sep-12 7:56am    
thank you for your help
_Amy 12-Sep-12 8:00am    
Always welcome. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900