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
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you please post the full method that the above code is contained in it.
0
 

Accepted Solution

by:
Karl66 earned 0 total points
Comment Utility
Closed.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

14 Experts available now in Live!

Get 1:1 Help Now