Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify that a column exists in the DataRow before reading its value

Tags:

How do I write code that reads a DataRow but, if filed in DataRow isn't there, it just skips it and moves on, like this for example:

string BarcodeIssueUnit; if (dr_art_line["BarcodeIssueUnit"].ToString().Length <= 0) {     BarcodeIssueUnit = ""; } else {     BarcodeIssueUnit = dr_art_line["BarcodeIssueUnit"].ToString(); } 

Now, the Column BarcodeIssueUnit can belong to the table but, in some cases, that column does not exist in the table. If it's not there and I read it, I get this error:

System.ArgumentException: Column `BarcodeIssueUnit`  does not belong to table Line. 

I just want to run a check if the column is there ok, let see the values, if it's not, just skip that part and go on.

like image 224
CrBruno Avatar asked May 03 '12 13:05

CrBruno


People also ask

How do I check if a DataRow column exists?

Although the DataRow does not have a Columns property, it does have a Table that the column can be checked for. Save this answer. Show activity on this post. You can use the DataColumnCollection of Your datatable to check if the column is in the collection.

How do you check if a column exists in a DataRow vb net?

You can use DataSet. Tables(0). Columns. Contains(name) to check whether the DataTable contains a column with a particular name.

How can you identify a column in a DataTable?

By using the Column name or Column index we can identify a column in a data table.

How check DataRow column value is null C#?

IsNull(Int32) Gets a value that indicates whether the column at the specified index contains a null value.


1 Answers

Check for column name using DataRow.Table.Columns. If there convert value else come out.

BarcodeIssueUnit = dr_art_line.Table.Columns.Contains("BarcodeIssueUnit")?                    dr_art_line["BarcodeIssueUnit"].ToString(): ""; 
like image 85
Nikhil Agrawal Avatar answered Oct 12 '22 15:10

Nikhil Agrawal