Solved

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

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

734 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