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
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

615 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