Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

How to check if a column with a given name exists in a datarow

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).

Upvotes: 5

Views: 34136

Answers (5)

Quethzel Diaz
Quethzel Diaz

Reputation: 641

The shortest solution.

 If dr.Table.Columns.Contains("columnname") Then
     'your code here
 End If

Upvotes: 1

Alexander Abakumov
Alexander Abakumov

Reputation: 14579

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.

Upvotes: 0

Leidr Garcia
Leidr Garcia

Reputation: 11

try:

if dr.Table.Columns("nameColumn") == null then

 //....

Upvotes: 1

Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

I got the answer.and its working . its:

  If dr.Table.Columns.Contains("columnname") = True Then
   --your work---
  End If

Upvotes: 16

Anuraj
Anuraj

Reputation: 19618

Try this

Dim dt As New DataTable
For Each dc As DataColumn In dt.Columns
    If dc.ColumnName = "" Then

    End If
Next

Upvotes: 1

Related Questions