Newbie: Detecting type of field contents in datareader

S

steve

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

W.G. Ryan eMVP

..GetValue may be an option - but if you need the schema information you can use the GetSchemaTable method (if you're using SqlConnection) http://www.knowdotnet.com/articles/schemas2.html or GetOleDbSchemaTable otherwise.

HTH,

Bill

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
I

Imran Koradia

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
....
....
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

William \(Bill\) Vaughn

Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
S

steve

what do you mean ???
I am using a datareader and a listview control

I dont think either have this method.



"William (Bill) Vaughn" <[email protected]> a écrit dans le message de Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
I

Imran Koradia

Bill,

Do you mean the dataadapter fill method? The OP mentioned using the datareader..Or is it something I'm missing?

Imran.
Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

W.G. Ryan eMVP

Steve:

Bill was referring to using an Adapter and a dataset/datatable to execute the query

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
what do you mean ???
I am using a datareader and a listview control

I dont think either have this method.



"William (Bill) Vaughn" <[email protected]> a écrit dans le message de Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
C

Cor Ligthert

Steve

Altough I find the datareader when you know the types better for the
listview, can the datatable when you do not know the types be as well a good
approach in my opinion.

And than loop through the datatable to get the needed values out in the most
simple way.

Cor
 
S

steve

should have thought about it!
Sorry .... :)

BTW thanx all of you for your valuable suggestions and your time!
"W.G. Ryan eMVP" <[email protected]> a écrit dans le message de Steve:

Bill was referring to using an Adapter and a dataset/datatable to execute the query

--
W.G. Ryan, MVP

www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
what do you mean ???
I am using a datareader and a listview control

I dont think either have this method.



"William (Bill) Vaughn" <[email protected]> a écrit dans le message de Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

William \(Bill\) Vaughn

Yes, you have it. The Fill method is supported by the DataAdapter class.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

what do you mean ???
I am using a datareader and a listview control

I dont think either have this method.



"William (Bill) Vaughn" <[email protected]> a écrit dans le message de Or, you could let ADO.NET do it--use Fill.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

You can use the GetFieldType method to retrieve the type of the field. Something like this:

If reader.GetFieldType(col) Is GetType(String) Then
' its a string type
Return reader.GetString(col)
ElseIf reader.GetFieldType(col) Is GetType(Integer) Then
' its an integer type
Return reader.GetInt32(col)

' add other types..
...
...
End If

It seems a bit verbose but it'll work.


hope that helps..
Imran.

Hi,

I have a few examples and things are quite clear with using a datareader (reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of fields and therefore the data type (Int, Date, String etc) since the SELECT string is constant.

My problem is with variable strings that get constructed at runtime. Then, since i do not know beforehand what exactly is my first,second, etc. field, I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know the type beforehand.

Is there a way around this? I really dont mind getting them all as text so is the an CType analogous method I could use . Something like casting BEFORE reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

W.G. Ryan eMVP

I think Bill's point was that there was more than one way to skin a cat and
using an Adapter and Fill would get you that information and the data you
needed in one step

--
W.G. Ryan MVP (Windows Embedded)

TiBA Solutions
www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com
Hi,

I have a few examples and things are quite clear with using a datareader
(reading data, naming column headers etc.), and populating a listview.

However: (!!!) In all of the examples we know beforehand the number of
fields and therefore the data type (Int, Date, String etc) since the SELECT
string is constant.

My problem is with variable strings that get constructed at runtime. Then,
since i do not know beforehand what exactly is my first,second, etc. field,
I CANNOT use :
myDataReader.GetString(0) or myData.GetInt32(0),etc. since io do not know
the type beforehand.

Is there a way around this? I really dont mind getting them all as text so
is the an CType analogous method I could use . Something like casting BEFORE
reading ??

ANY help would be appreciated,

Thanx

-steve
 
W

William \(Bill\) Vaughn

Puleeeze. I love cats...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
W

W.G. Ryan eMVP

Say it's not so. I heard that they throw you out of the Jedi Master Academy
for being a cat lover. First John Robbins, now you - this is going to give
me a nervous breakdown.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top