views:

56

answers:

1

I have my strings in a String array, my Data in a datatable.

What I have to do is, select a single row using datatable.select(string) method and delete it from the datatable.

this is what I am doing, and its working fine many times but its returning an error if I change the array frequently. I have checked the datarows in the datatable and also the matching string. they are there as they are supposed to be. But some times it still returns null. But when I checkback, the data is exactly as I want it to be.

             for (int k = 0; k < p; k++)
                {
                    string tempMapString = "ID =" + IDArray[k];
                  try
                    {
                        DataRow[] rows = myTable_2_ForCBL.Select(tempMapString);

                        //MessageBox.Show(IDArray[k].ToString(), "ID Array Element");
                        MessageBox.Show(tempMapString + "  " + rows.Length.ToString(), "No of Rows mapped are:");



                        if (rows.Length > 0)
                        {
                            foreach (DataRow row in rows)
                            {
                                row.Delete();
                            }
                        }
                    }
                    catch (Exception err2)
                    {
                        MessageBox.Show(err2.Message);
                    }

                }

let me know if I am missing something. There columns in the datatable are "ID" and "LocationName". ID is like the primary key and is the valuemember of the checkedbox list.

A: 

I got the answer. Its a Stupid BUG which Microsoft ignored in their documentation.

you need to put single quote (') for the string. so the line would be

DataTable1.Select("ID= '"+tempstring+"'");

Yash
The documentation for `Select` says "use the same rules that apply to the DataColumn class's Expression property". The documentation for `Expression` says, "When you create an expression for a filter, enclose strings with single quotation marks: `"LastName = 'Jones'"`"
Lee