Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Casting Integer to String

Status
Not open for further replies.

vpekulas

Programmer
Jan 8, 2002
154
CA
I'm using the following to get the table structure:

Code:
            SQL = "SELECT column_name, data_type, character_maximum_length FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" & strTABLE & "'"
            Call OPEN_DB()
            Dim MyCMD As New OleDb.OleDbCommand(SQL, MyConn)
            Dim MyRead As OleDb.OleDbDataReader
            MyRead = MyCMD.ExecuteReader
            Dim iItem As New ListViewItem(), intI As Int16
            While Not MyRead.Read = False
                strVALUE(0) = MyRead.GetString(0)
                strVALUE(1) = MyRead.GetString(1)
                strVALUE(2) = MyRead.GetInt32(2).ToString
                iItem = New ListViewItem(strVALUE)
                lstColumns.Items.Add(iItem)
            End While
            MyRead.Close()
            MyConn.Close()
            MyConn.Dispose()


The problem I have is that no matter what I try I can not get the 3rd value (character_maximum_length)

strVALUE(2) = MyRead.GetInt32(2).ToString

The erro I get is: Specified cast is not valid.


And I'm sure the character_maximum_length is in Integer


"Taxes are the fees we pay for civilized society" G.W.
 
Perhaps is is actially Int16, the OLD definition of integer.
Just for kicks, try
dim int as short
int = MyRead.GetInt16(2)
strVALUE(2) = int.toString
then

dim int as int
int = MyRead.GetInt32(2)
strVALUE(2) = int.toString



Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
I did just that, thinking it could be int16, eventhough the return datatype is int32, doesn't work :) Same error.

"Taxes are the fees we pay for civilized society" G.W.
 
I have this code that may be useful with changes and viewed in Debug Mode.

Private Function GetSchemaTable(ByVal objConn As OleDbConnection, _
ByVal strCommand As String) _
As DataTable
Dim objDBComm As New OleDbCommand()
objDBComm.Connection = objConn
objDBComm.CommandText = strCommand
objDBComm.CommandType = CommandType.TableDirect
Return (GetRdr(objConn, strCommand, CommandBehavior.SchemaOnly).GetSchemaTable)
End Function


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
casting is odd, have you tryed...

Dim strValue(2) As String = Ctype(myReader.GetInt32(2), Integer)

...in my example i had a drop down list with an integer value that was a string that i had to make into an integer, otherwise i got "specified cast is invalid"

Dim tstatID As DropDownList = e.Item.Cells(6).FindControl("IncStatusDDL")
Dim getStatID As String = tstatID.SelectedItem.Value
Dim statID As Integer = Ctype(getStatID, Integer)
Dim IncidentID As Integer = CType(e.Item.Cells(2).Controls(0), TextBox).Text
 
I'm completely lost, no matter what I try I can not get it to work ... thanks for the suggestions above ... still not working.

Apparently the datatype for character_maximum_length is dbtype_I4 returned using GetDatTypeName, the GetFieldType
returns system.int32. When I try to get the value using MyRead.GetInt32(2) I get "Cast is not valid" :(

The error is not converting the returned value to the string I can use, but rather getting the value from the DB.

"Taxes are the fees we pay for civilized society" G.W.
 
Dim strVal2 As Int32 = Ctype(myReader.GetInt32(2), Int32)
Dim strVal3 As Integer = Ctype(strVal2, Int32)
Dim strValue(2) As String = Ctype(strVal3, Integer)

pull out the value as it is first, then cast the value to string, or even integer, then to string.

Just thinking it might take a couple of castings.
Interesting to know what works eventually. Ive only a few months of experience.
 
Nope, still nothing, this fails on the first line:
Dim strVal2 As Int32 = CType(MyRead.GetInt32(2), Int32)

The reason is that even though the character_maximum_length is supposed to be an int32 datatype, it's not. I even tried to convert (cast) the character_maximum_length from the SQL string to a varchar(25) but that doesn't help either.

Is there any other way of getting the max length of give field ?


"Taxes are the fees we pay for civilized society" G.W.
 
Check this out...


Im thinking as such...

SQL = "DECLARE @Character_Maximum_Length int
SELECT column_name, data_type, character_maximum_length FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" & strTABLE & "'"
Call OPEN_DB()
Dim MyCMD As New OleDb.OleDbCommand(SQL, MyConn)
Dim MyRead As OleDb.OleDbDataReader
MyRead = MyCMD.ExecuteReader
Dim iItem As New ListViewItem(), intI As Int16
While Not MyRead.Read = False
strVALUE(0) = MyRead.GetString(0)
strVALUE(1) = MyRead.GetString(1)
strVALUE(2) = @CHARACTER_MAXIMUM_LENGTH.ToString
iItem = New ListViewItem(strVALUE)
lstColumns.Items.Add(iItem)
End While
MyRead.Close()
MyConn.Close()
MyConn.Dispose()
 
Nothing worked until I've used

strVALUE(2) = MyRead.GetValue(2) & ""

That's the only thing that wors :) Who knows why .....
Thanks guys!

"Taxes are the fees we pay for civilized society" G.W.
 
Either null or whtever the length is as a string

"Taxes are the fees we pay for civilized society" G.W.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top