1

I'm populating a DataTable object from an Excel worksheet. I'm not counting on the user entering data correctly, and I'd like to delete rows that have a null value in column A. I've searched around quite a bit, and it looks like everyone is doing this with a for loop. Here's the problem: 757,000 rows. If data is not formatted properly that DataTable gets filled up with Excel's max of 1048575. I don't need to check each value individually. In SQL server you can write:

DELETE table WHERE columnA IS NULL

I considered excluding nulls on the DataTable fill, but I couldn't get this to work reliably as I don't know the name of column A until it's in the DataTable and sometimes the column name has a \n character in the middle which throws syntax errors.

How can I accomplish the same sort of thing without a loop?

3
  • Do you want to delete the rows form the source workbook or just not show them in the datatable? How are you reading from excel? Commented Feb 11, 2015 at 20:09
  • can you not implement the Select() Method of the DataTable to filter down and perhaps populate a DataTable from there..? also IN SQL Server Your syntax is incorrect.. it's DELETE [FROM TABLE NAME] WHERE Clause Commented Feb 11, 2015 at 20:12
  • @DStanley really I just want them to not be in the datatable. Commented Feb 11, 2015 at 20:34

2 Answers 2

4

How about creating a new, clean DataTable from your first one?

DataTable t = new DataTable(); //actually your existing one with bad records
DataTable newTable = t.Select().Where(x => !x.IsNull(0)).CopyToDataTable();
Sign up to request clarification or add additional context in comments.

Comments

0

Just use Linq...

var newData = (from d in DataTable //this would be your datatable. 
              where d.columnA != NULL
              select d.*).CopyToDataTable<DataRow>();

Then just use your newData variable, which holds your newly populated datatable.

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.