Solved

How to get the data field type when retrieving data via a datareader?

Posted on 2013-01-29
3
264 Views
Last Modified: 2013-01-29
I need to get the data field type of a column in a data row of a data reader. Please see the attache code. The problem arises when using "GetFieldType" because it requires the ordinal position which is unknown. I do have the column name.

The query can change and thus the ordinal position of a column can change.

The reason I need it to work this way is for the simple addition of control to the form and adding the column name to the tag of that control.

    Private Function GetMyData() As Boolean
        Dim MyConnection As New SqlConnection
        Dim MyCommand As New SqlCommand
        Dim MyDataReader As SqlDataReader = Nothing
        Dim sQuery As String = ""
        Dim sColumnName As String = ""
        Dim c As Control

        sQuery = "select SomeColumn, SomeOtherColumn from tblSomeTable "
        MyConnection.ConnectionString = "my connection string"
        MyConnection.Open()
        MyCommand.Connection = MyConnection
        MyCommand.CommandText = sQuery
        MyDataReader = MyCommand.ExecuteReader
        If MyDataReader.HasRows Then
            If MyDataReader.Read Then
                For Each c In Me.Controls
                    If Not c.Tag Is Nothing Then
                        'The tag contains the column name'
                        sColumnName = c.Tag.ToString
                        If TypeOf c Is TextBox Then
                            If MyDataReader.GetFieldType(sColumnName) Is GetType(System.Single) Then
                                c.Text = Format(CSng(MyDataReader.Item(sColumnName)), "####0.00")
                            Else
                                c.Text = MyDataReader.Item(sColumnName).ToString
                            End If
                        End If
                    End If
                Next
            End If
        End If
        MyDataReader.Close()
        MyDataReader = Nothing
        MyCommand = Nothing
        MyConnection.Close()
        MyConnection = Nothing
        GetMyData = True
    End Function

Open in new window

0
Comment
Question by:etsellinc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38831736
Using dataReader.GetSchemaTable():
http://support.microsoft.com/kb/310107
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 38831740
You can use MyDataReader.GetOrdinal("ColumnName") to get the column ordinal


http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.getordinal.aspx
0
 

Author Closing Comment

by:etsellinc
ID: 38831793
Thank you! It works into the solution with minimal code.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question