Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can you tell if a column exists?

Status
Not open for further replies.

Glowworm27

Programmer
May 30, 2003
587
US
Hello all

I have a dataset, and I need to know if a Column(field) exists in the dataset before I try to get data from it.

Code:
If dsMyDataset.tables(0).Rows(0).Item("estNum") <> 0 Then
   bIsBFRCREcord = True
End if

I get an error that column "estnum" does not exists in the dataset.

I dont want the user to get an error if they don't have that column in thier database.

George Oakes
Check out this awsome .Net Resource!
 
BoulderBum,

Nice try, but that will NOT work.

First in ASP.net you cannot use Null, it is no longer used.

You must write the if statement this way.
Code:
 If IsDBNull(.Rows(0)("estNum")) Then
   ' Do something
 End if
Secondly this will still throw the same error: "Column estNum does not exists in Table"

what your statement does is check the value in the column, and because it doesn't exist in the table you get the error.

I need a way to check if the column exists first, so I can then check if it has a value after.

Thanks for trying

George Oakes
Check out this awsome .Net Resource!
 
What about this:
Code:
If ds.Tables(0).Columns.Contains("estNum") Then
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
this should work

Code:
if ds.tables(0).columns("estnum") = true then
    bIsBFRCREcord = True
end if

or cast your dataset to a datatable and then you could do

Code:
dim dt as datatable = new datatable
dt = ds.tables(0)
if dt.Columns("estnum") = true then
 'do your stuff
end if
 
First in ASP.net you cannot use Null, it is no longer used."

Actually "null" is used in C# (my primary language). I just forgot the VB.NET translation:

If ds.Tables(0).Rows(0)("estNum") <> Null Then
-should have been-
If ds.Tables(0).Rows(0)("estNum") <> Nothing Then

In this case, you don't want to use DBNull because you're checking a row value, you're checking whether the column object reference is set to an instance.

[COLOR=blue gainsboro]
Get a FREE iPod by helping me get mine! Click my referrer link:

More about the company and deal:
[/color]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top