Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
386 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 80 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

927 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