Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Import a txt file into a DataGridView and TextBox 20 52
get column names from table in vb.net 8 49
export data table to excel in vb.net 10 36
Footer for each row on Gridview 2 32
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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