I want to insert a value from loop in datarow so before entering value in datarow, I want to check that a perticular column NAME exist in table or not.
Please tell me how can I check it. (vb.net preferred).
Asked
Active
Viewed 3.4k times
5

Ḟḹáḿíṅḡ Ⱬỏḿƀíé
- 2,128
- 5
- 23
- 33

Dr. Rajesh Rolen
- 14,029
- 41
- 106
- 178
5 Answers
16
I got the answer.and its working . its:
If dr.Table.Columns.Contains("columnname") = True Then
--your work---
End If

Dr. Rajesh Rolen
- 14,029
- 41
- 106
- 178
-
1This part of statement `= True` is not necessary. The contains method return a boolean. – Quethzel Diaz Apr 19 '17 at 17:36
1
Try this
Dim dt As New DataTable
For Each dc As DataColumn In dt.Columns
If dc.ColumnName = "" Then
End If
Next

Anuraj
- 18,859
- 7
- 53
- 79
1
The shortest solution.
If dr.Table.Columns.Contains("columnname") Then
'your code here
End If

Quethzel Diaz
- 621
- 1
- 11
- 26
0
Here is another way to find out if a column exists:
If dataRow.Table.Columns("ColumnName") IsNot Nothing Then
-- Your code if a column exists
End If
See this answer for further reference when this approach might be handier than the Contains("ColumnName")
one.

Community
- 1
- 1

Alexander Abakumov
- 13,617
- 16
- 88
- 129