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 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;
}
Replies
Know the answer? Post it — somebody with the same question will find it here.