Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre> string[] meassteps = { "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "B10" };

                foreach (string ms in meassteps)
                {
                    conStr = String.Format(conStr, filePath, "Yes");
                    OleDbConnection connExcel = new OleDbConnection(conStr);
                    OleDbCommand cmdExcel = new OleDbCommand();
                    OleDbDataAdapter oda = new OleDbDataAdapter();

                    cmdExcel.Connection = connExcel;

                    DataTable sheets = GetSchemaTable(conStr);
                    connExcel.Open();

                     int cnt = 0;
                     foreach (DataRow r in sheets.Rows)
                         {
                            if (cnt > 0) break;
                            string sheetName = r[2].ToString();

                            DataTable dt = new DataTable(sheetName);


                            cmdExcel.CommandText = "SELECT * From [" + sheetName + "]";
                            oda.SelectCommand = cmdExcel;
                            oda.Fill(dt);

                            dtArray[cnt] = dt;

                            //dt.DefaultView.RowFilter = "measstep";

                            DataRow[] row;
                            row = dt.Select(ms);
                            if (row != null & row.Length > 0)
                            {
                                dt = row.CopyToDataTable();
                                dt = dt.DefaultView.ToTable();

                            }

                }


Hi all. I have a code here which I have created a new string array to search for the meassteps from "B1" to "B10" in my datatable which has a column named "measstep".
What my code is supposed to do is to filter out the measstep and put it in another datatable. If "B1" does not exist in the column "measstep", then search for the next string which is "B2" and filter B2 out and so on, until "B10".

However, I'm having an error that says "Cannot find Column[B1]" when string ms = B1.

How do I write my code such that I'm searching the measstep that is in my datatable?

What I have tried:

I have tried putting my string array into the foreach loop so that the string array exist in my datatable.
Posted
Updated 6-May-18 21:17pm

DataTable.Select doesn't work like that: DataTable.Select Method (String) (System.Data)[^] - you are supplying a search criteria thjat is just "B1", or "B2", etc. - which isn't a search. You need to replace that with "ColumnName = 'B1'" and so on.

Quote:
Oh, so I have to replace the string array[] to ColumnName = B1 to B10?


It works like this:
DataTable dt = new DataTable();
dt.Columns.Add("A1");
dt.Columns.Add("A2");
dt.Rows.Add("r1", "B1");
dt.Rows.Add("r2", "B2");
dt.Rows.Add("r3", "B1");
dt.Rows.Add("r4", "B2");
DataRow[] rows = dt.Select("A2 = 'B2'");
foreach (DataRow row in rows)
    {
    Console.WriteLine("{0}:{1}", row[0], row[1]);
    }
Gives:
r2:B2
r4:B2
So I'd change your code like this:
DataRow[] row;
row = dt.Select(string.Format("measstep ='{0}'", ms));
 
Share this answer
 
v3
Comments
Member 13650651 7-May-18 2:44am    
Oh, so I have to replace the string array[] to ColumnName = B1 to B10?
OriginalGriff 7-May-18 3:33am    
Answer updated
Member 13650651 7-May-18 3:49am    
Thanks so much OriginalGriff! Appreciate it!
OriginalGriff 7-May-18 4:13am    
You're welcome!
First of all: if, you would like to use foreach(...) loop, i'd move few lines of your code outside the foreach(...) loop due to the efficiency. Those are the lines, where you open Excel connection and fill in datatable.
Second of all: you don't need second loop (foreach (DataRow r in sheets.Rows)), because you're using Select method to filter data!
C#
foreach(string ms in meassteps)
{
	DataRow[] msrows= dt.Select(string.Format("measstep Like %{0}%", ms));
}


Another way is to use Linq.
If you would like to filter datatable by comparing each row to the collection of strings, you're able to achieve that by using Where() + Any():
C#
var filteredrows = dt.AsEnumerable()
	.Where(x=> meassteps.Any(y=> x.Field<string>("measstep").Contains(y)))
	.ToList();

or cross join:
C#
var filteredrows1 = from dr in dt.AsEnumerable()
	from ms in meassteps
	where dr.Field<string>("measstep").Contains(ms)
	select dr;
 
Share this answer
 
Comments
Member 13650651 7-May-18 3:48am    
Thank you Maciej Los, this works! Appreciate it alot.
Maciej Los 7-May-18 4:58am    
You're very welcome.
Member 13650651 7-May-18 3:48am    
Another question, I have the code to filter out each 'ms'. Do i put the code into the foreach loop?
Maciej Los 7-May-18 4:58am    
See the first part of my answer. ;)
Member 13650651 7-May-18 3:58am    
if B1 is null, how do i continue the loop and run my filter code for B2 that is not null?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900