Solved

C# VS2008 Windows App: Get error when running method twice "A column named 'Name' already belongs to this DataTable"

Posted on 2008-06-23
4
367 Views
Last Modified: 2013-12-16
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
0
Comment
Question by:Karl66
  • 3
4 Comments
 
LVL 13

Expert Comment

by:iHadi
ID: 21851119
Hi Karl66

Try the attached code:
PerformanceCounter pc = new PerformanceCounter();
 
public Process GetProcess(string hostname, string processName)
{
    Process[] ps = Process.GetProcessesByName(processName, hostname);
 
    if (ps.Length == 0)
        return null;
    else
        return ps[0];
}
 
public bool IsProcessAlive(string hostname, string processName)
{
    Process p = GetProcess(hostname, processName);
 
    if (p == null)
    {
        return false;
    }
    else
    {
        return !p.HasExited;
    }
}
 
public string GetProcessCPU(string hostname, string processName)
{
    string str = "";
 
    pc.CategoryName = "Process";
    pc.CounterName = "% Processor Time";
    pc.InstanceName = processName;
    pc.MachineName = hostname;
 
    try
    {
        str = pc.NextValue().ToString();
    }
    catch
    {
        str = "Error";
    }
 
    return str;
}
 
public string GetProcessRAM(string hostname, string processName)
{
    Process p = GetProcess(hostname, processName);
 
    if (p == null)
    {
        return "Error";
    }
    else
    {
        return p.PagedMemorySize64.ToString();
    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:iHadi
ID: 21851140
Sorry, the previous post was for another question.

I think it should be deleted from this question.
0
 
LVL 13

Assisted Solution

by:iHadi
iHadi earned 20 total points
ID: 21851178
Can you please post the full method that the above code is contained in it.
0
 

Accepted Solution

by:
Karl66 earned 0 total points
ID: 21861301
Closed.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question