Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using below code to upload txt file data separated by '|' delimiter in datagrid view. For the column[0](Emp_ID), datatype is numeric. Many time we found string or special charactor(*,#,@,$ etc.) for the first column of the txt file. While we have to upload only number data in the datagrid for first column from txt file.
I am looking for a code here which will actually prevent string or special character values to upload in the datagrid. It should invoke with line number error while uploading, so that we can remove that particular string related data from the txt file.


public class User
{

public string Emp_Id { get; set; }
public DateTime Atten_Date { get; set; }
public DateTime Atten_Punch_Date { get; set; }
public string Atten_Project_code { get; set; }
public string Atten_states { get; set; }

public static List<user> LoadUserListFromFile(string location)
{

Stream mystream;
OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((mystream = opentext.OpenFile()) != null)
{
location = opentext.FileName;
// string filetext = File.ReadAllText(strfilename);
}
}

var users = new List<user>();

try
{
foreach (var line in File.ReadAllLines(location))
{

var columns = line.Split(new[] { '|' });

users.Add(new User

{
Emp_Id = columns[0].Trim(),
Atten_Date = Convert.ToDateTime(columns[1]),
Atten_Punch_Date = Convert.ToDateTime(columns[2]),
Atten_Project_code = columns[4].Trim(),
Atten_states = columns[3].Trim()

}


);

}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Please select file to upload");
}
return users;

}
Posted
Comments
[no name] 2-Jul-15 14:20pm    
So use TryParse to attempt to convert the string to a numeric and if it fails then you have your culprit.
Member 11745361 2-Jul-15 14:25pm    
But i want remove that particular row from txt manually for which Emp_ID has other than numeric data.

Please suggest !
[no name] 2-Jul-15 14:31pm    
Okay... suggest what? If you are removing the data from the file manually then what is there to "suggest"? Open the file with whatever editor you have, select the data you want to delete, delete it, save the file.

1 solution

Hello!

You can use REGEX for this.

C#
var columns = line.Split(new[] { '|' });

Emp_Id = new System.Text.RegularExpressions.Regex(@"\D").Replace(columns[0].Trim(),"");

@"\D" Matches any character other than a decimal digit...
 
Share this answer
 
Comments
Member 11745361 3-Jul-15 14:45pm    
I tried to use below code as suggested but still it upload all the string and special character int the grid.

Emp_Id = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z0-9]$").Replace(columns[0].Trim(), ""),

I need a prompt from the system before uploading that row, which having string or special character in the uploading file.

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