vinod chand

vinod chand

  • NA
  • 16
  • 9.5k

Search for string or spcial char. in txt file for upload

Jul 3 2015 2:31 PM
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 prompt for 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. And try to upload again.

In attachment , uploading file in included for reference.
 
 
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 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();

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;

}