4

I have written following code to compare the records of DataSet (i.e.) record of one column. And I am getting following Exception:

ex:" Index was outside the bounds of the array."

  public void GetRunningTask()
  {
      // Process[] lstprocess = Process.GetProcesses();
      conn=new SqlConnection("Data Source=.; Initial Catalog='TTES'; Integrated Security=SSPI;");
      da=new SqlDataAdapter("Select AppName from LRNSetting", conn);
      ds=new DataSet();
      da.Fill(ds,"LRNSetting");

      // Process[] lstprocess = Process.GetProcesses();
      for (int k = 0; k < ds.Tables[0].Rows.Count; k++)
      {
        Process[] lstprocess = Process.GetProcesses();
        // DataRow dr=ds.Tables[0].Rows.Cast<DataRow>().Single(row=>row["AppName"])

        var pro = from p in lstprocess
                 //where p.ProcessName.Contains("LRCDual")
                 //where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray)  //added temporary
                 where (p.ProcessName.Contains(ds.Tables[0].Rows[0].ItemArray[k].ToString()))
                 select p;
       }
  }
0

7 Answers 7

2

Although you made the iteration on ds.Tables[0].Rows.Count but you are using the counter for ItemArray not for Rows as expected,

ds.Tables[0].Rows[0].ItemArray[k].ToString()

I suggest you to review your logic

Sign up to request clarification or add additional context in comments.

4 Comments

where (p.ProcessName.Contains(ds.Tables[0].Rows[0].ItemArray[k].ToString())) //Here i want to compare the process Name getting from GetProcess with the database records where the records of dataset containing exe names(i.e. process names that i have putted mannunally).
So , I would like to select those process that should also present in my database records otherwise.
Eg. ConsoleApplication1.exe , ConsoleApplication2.exe these process are running in system and i have only record of "ConsoleApplication1" then it should select only this record.
make another loop for the ItemArray, so your code will be like 'where (p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray[m].ToString())) ' where 'm' is a new iteration variable that loops for all itemArrays in Row[k]
1

You seem to have a few issues with your code. First up, as others have said, you are using index k with the bound k < ds.Tables[0].Rows.Count but you are using it against ds.Tables[0].Rows[0].ItemArray[k]. They are two different things.

You are better off not using indexes like this. You are using LINQ for part of your code, but you could use it for the rest.

Also you seem to not want to dispose of any of your disposable objects. You must ensure all disposables are disposed of.

So, try this:

using (var conn = new SqlConnection("Data Source=.; Initial Catalog='TTES'; Integrated Security=SSPI;"))
{
    using (var da = new SqlDataAdapter("Select AppName from LRNSetting", conn))
    {
        using (var ds = new DataSet())
        {
            da.Fill(ds,"LRNSetting");

            var appNames =
                ds
                    .Tables[0]
                    .Rows
                    .Cast<DataRow>()
                    .Select(x => x[0].ToString())
                    .ToArray();

            var pro =
                from p in Process.GetProcesses()
                where appNames.Any(x => p.ProcessName.Contains(x))
                select p;
        }
    }
}

Comments

1

Simple Linq query, Make DataRowCollection enumerable, apply select to get list of given column with process name and compare with original process name:

lstprocess.Where(p=>ds.Tables[0].Rows.AsEnumerable.Select(row=>row["ColumnName"].ToString()).Contains(p.ProcessName))

Comments

1

You need to review your code.

You made the iteration on table's rows count but you are using the counter for ItemArray not for rows as expected.

Replace this code

var pro = from p in lstprocess
             //where p.ProcessName.Contains("LRCDual")
             //where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray)  //added temporary
             where (p.ProcessName.Contains(ds.Tables[0].Rows[0].ItemArray[k].ToString()))
             select p; 

with this:

var pro = from p in lstprocess
             //where p.ProcessName.Contains("LRCDual")
             //where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray)  //added temporary
             where (p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray['ColumnName'].ToString()))
             select p;

Comments

0

Try

var pro = from p in lstprocess
          where (p.ProcessName.Contains(ds.Tables[0].Rows[k][0].ToString()))
          select p;

Comments

0

You need to select data from current row on which loop is currently iterating. Also you may want to get data from a specific column so you need to specify column name like ds.Tables[0].Rows[k]["columnName"].ToString()). Replace "columnName" with actual column name.

var pro = from p in lstprocess
          //where p.ProcessName.Contains("LRCDual")
          //where p.ProcessName.Contains(ds.Tables[0].Rows[k].ItemArray)  //added temporary
          where (p.ProcessName.Contains(ds.Tables[0].Rows[k]["columnName"].ToString()))
          select p;

Comments

0

Using ItemArray[k] means you assumed you have k columns but as your code shows, you have k rows.

So This must be what you are looking for:

//Getting all table cells for every column and row as string
var tableValues = ds.Tables[0].AsEnumerable()
                              .SelectMany(i => i.ItemArray.Select(j => j.ToString()))
                              .ToList();

Process[] lstprocess = Process.GetProcesses();

var pro = from p in lstprocess
          where tableValues.Any(i => p.ProcessName.Contains(i))
          select p;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.