Pass / Store data type

M

Mike Hoff

I am trying to write a class that will store info about database fields for
building UPDATE / INSERT commands later on. I cannot seem to get the sytax
correct to pass and store the data type of the field. My class looks like:

Public Class DataTag
Private m_Text As String
Private m_FieldName As String
Private m_ParamName As String
Private m_DataType As System.Type 'the variable type of this field

Public Sub New(ByVal sText As String, ByVal sField As String, ByVal sParam
As String, ByVal typeData As System.Type)
m_Text = sText
m_FieldName = sField
m_ParamName = sParam
m_DataType = typeData
End Sub

When I try to define an object with:

txtName.Tag = New DataTag(txtName.Text, "Name", "@Name", System.String)

System.String shows error: 'String' is a type in 'System' and cannot be used
in an expression. I have also tried using a GetType property on a string,
but that has failed also. How can I pass and store the data type of the
field? I want to do this so that I can later loop through the text boxes
and do something like...

MySQLCommand.Parameters.Add(dataTagCurrent.paramName,
dataTagCurrent.DataType).Value = txtLoop.Text

Or something similar based on the control/data type, and also build the SQL
Command string.

Any help appreciated - thanks,
Mike
 
I

Imran Koradia

Is this not working for you?

txtName.Tag = New DataTag(txtName.Text, "Name", _
"@Name", GetType(System.String))

Imran.
 
K

Ken Dopierala Jr.

Hi Mike,

Instead of Private m_DataType As System.Type

Try: Private m_DataType as Integer

Then when you pass to the function use SqlDbType.VarChar or whatever type it
is you are using. SqlDbType is just a class that holds Constants. Remember
to import System.Data.SqlClient wherever you use it. Good luck! Ken.
 
G

Guest

Hi Mike and Ken,

I had a similar question and was hoping that either of you could help me. It
is probably a dumb queston but I was trying to determine the type of column
coming back from a resultset to format a date but I don't know what to
compare it to for the type.

I was hoping to do this

If MySqlDataReader.GetFieldType(0) = <DATETIME constant>

trying SqlDbType types and GetType() on the RHS with no luck.

Thanks for your time
Regards
Amelia
 
K

Ken Dopierala Jr.

Hi Amelia,

There are a couple ways you can do this. The first is instead of using
GetFieldType() use:

MySqlDataReader.GetDataTypeName(0)

This will return a string of the data type the field contains. For example
and Integer = 'int'. Another way to test for a date is to test against the
actual field:

If (IsDate(MySqlDataReader("DateField")) Then
....process as date
End If

Good luck! Ken.
 
G

Guest

Hi Ken,

Thanks so much for your help and time - it really saved my bacon! Still
trying to work my way around .Net but determined to know all the classes soon
AND how to use them!

:0)

Amelia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top