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
360 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now