Solved

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

Posted on 2013-01-29
3
257 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
3 Comments
 
LVL 42

Expert Comment

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

Accepted Solution

by:
CodeCruiser earned 500 total points
Comment Utility
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
Comment Utility
Thank you! It works into the solution with minimal code.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now