Karl66
asked on
C# VS2008 Windows App: Get error when running method twice "A column named 'Name' already belongs to this DataTable"
C# VS2008 Windows App: Get error when running method twice
"A column named 'Name' already belongs to this DataTable"
Is there an easy way to avoid getting this when I let the user crun the method twice? Here is the code that errors when repeated. I understand the nature of the message looking for best "practice"\work around.
foreach (FileInfo file in files)
{
DataRow dr = dt.NewRow();
//get details of each file using file object
dr[Name] = file.FullName;
dr[Size] = file.Length;
dr[Ext] = file.Extension;
dr[Created] = file.LastWriteTime;
dt.Rows.Add(dr);
Karl66
"A column named 'Name' already belongs to this DataTable"
Is there an easy way to avoid getting this when I let the user crun the method twice? Here is the code that errors when repeated. I understand the nature of the message looking for best "practice"\work around.
foreach (FileInfo file in files)
{
DataRow dr = dt.NewRow();
//get details of each file using file object
dr[Name] = file.FullName;
dr[Size] = file.Length;
dr[Ext] = file.Extension;
dr[Created] = file.LastWriteTime;
dt.Rows.Add(dr);
Karl66
Sorry, the previous post was for another question.
I think it should be deleted from this question.
I think it should be deleted from this question.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try the attached code:
Open in new window