Solved

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

Posted on 2013-01-29
3
258 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DataGridview Currency Formating? 22 52
Closing all open child forms. 4 38
vb.net - How to check if current user is an administrator? 6 34
C# Single Form 8 27
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

912 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

20 Experts available now in Live!

Get 1:1 Help Now