Solved

System32Int Error

Posted on 2016-11-11
8
44 Views
Last Modified: 2016-11-18
I have some code I am running and I get this error when I run it:

Unable to cast object of type 'SystemInt32' to type 'System.String'

This is the procedure that is throwing the error:

            Dim column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23 As String
            Dim oWrite2 As System.IO.StreamWriter

            Dim intCount As Integer = 0

            cmd.Connection = cn
            cn.Open()
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "EXECUTE udp_TEMPERED_PO '" & strBatch & "'"
            cmd.ExecuteNonQuery()

            cmd.CommandText = "SELECT COUNT(*) AS BATCHCOUNT FROM PCT_TEMPERED_PO_FINAL"
            intCount = cmd.ExecuteScalar()

            cn.Close()

            If intCount > 0 Then
                Dim selectSQL2 As String = "SELECT Bin, Assorted_Bin, Item_Number, Glass,itemdesc, Order_Number, CustName, Line_Number, Batch_Number, status, WinPart, WinPos, xwpFixedUnitWidth, xwpFixedUnitHeight,xwpVentUnitWidth,xwpVentUnitHeight,Quantity, xwpTempered,FUW_FRACT, FUH_FRACT, VUW_FRACT, VUH_FRACT,RECORD FROM PCT_TEMPERED_PO_FINAL"
                 Dim t As Char = vbTab

                Dim com2 As New SqlCommand(selectSQL2, cn)
                com2.CommandType = CommandType.Text
                cn.Open()

                strPath = "\\thor\Production\TemperedPO\" & strBatch & ".txt"
                oWrite2 = System.IO.File.CreateText(strPath)

                Dim myreader2 As SqlDataReader = com2.ExecuteReader

                Dim schemaTable As DataTable = myreader2.GetSchemaTable()
                Dim collist = From eachrow In schemaTable.Rows() Select eachrow(0)

                Dim strcolname As String = String.Join(vbTab, collist)
                oWrite2.WriteLine(strcolname)

                While myreader2.Read
                    column_1 = CStr(myreader2.GetString(0))
                    column_2 = CStr(myreader2.GetString(1))
                    column_3 = CStr(myreader2.GetString(2))
                    column_4 = CStr(myreader2.GetString(3))
                    column_5 = CStr(myreader2.GetString(4))
                    column_6 = CStr(myreader2.GetString(5))
                    column_7 = CStr(myreader2.GetString(6))
                    column_8 = CStr(myreader2.GetString(7))
                    column_9 = CStr(myreader2.GetString(8))
                    column_10 = CStr(myreader2.GetString(9))
                    column_11 = CStr(myreader2.GetString(10))
                    column_12 = CStr(myreader2.GetString(11))
                    column_13 = CStr(myreader2.GetString(12))
                    column_14 = CStr(myreader2.GetString(13))
                    column_15 = CStr(myreader2.GetString(14))
                    column_16 = CStr(myreader2.GetString(15))
                    column_17 = CStr(myreader2.GetString(16))
                    column_18 = CStr(myreader2.GetString(17))
                    column_19 = CStr(myreader2.GetString(18))
                    column_20 = CStr(myreader2.GetString(19))
                    column_21 = CStr(myreader2.GetString(20))
                    column_22 = CStr(myreader2.GetString(21))
                    column_23 = CStr(myreader2.GetString(22))
                    oWrite2.WriteLine("{0}" & t & "{1}" & t & "{2}" & t & "{3}" & t & "{4}" & t & "{5}" & t & "{6}" & t & "{7}" & t & "{8}" & t & "{9}" & t & "{10}" & t & "{11}" & t & "{12}" & t & "{13}" & t & "{14}" & t & "{15}" & t & "{16}" & t & "{17}" & t & "{18}" & t & "{19}" & t & "{20}" & t & "{21}" & t & "{22}", column_1, column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23)
                End While
                oWrite2.Close()
                cn.Close()
            End If
        Next

        frmPauseMsg.Hide()
        MsgBox("All selected batches have been moved to production.")

        Me.Close()

Open in new window


By selectively commenting out code and running the program I have narrowed the offending code down to this:

                While myreader2.Read
                    column_1 = CStr(myreader2.GetString(0))
                    column_2 = CStr(myreader2.GetString(1))
                    column_3 = CStr(myreader2.GetString(2))
                    column_4 = CStr(myreader2.GetString(3))
                    column_5 = CStr(myreader2.GetString(4))
                    column_6 = CStr(myreader2.GetString(5))
                    column_7 = CStr(myreader2.GetString(6))
                    column_8 = CStr(myreader2.GetString(7))
                    column_9 = CStr(myreader2.GetString(8))
                    column_10 = CStr(myreader2.GetString(9))
                    column_11 = CStr(myreader2.GetString(10))
                    column_12 = CStr(myreader2.GetString(11))
                    column_13 = CStr(myreader2.GetString(12))
                    column_14 = CStr(myreader2.GetString(13))
                    column_15 = CStr(myreader2.GetString(14))
                    column_16 = CStr(myreader2.GetString(15))
                    column_17 = CStr(myreader2.GetString(16))
                    column_18 = CStr(myreader2.GetString(17))
                    column_19 = CStr(myreader2.GetString(18))
                    column_20 = CStr(myreader2.GetString(19))
                    column_21 = CStr(myreader2.GetString(20))
                    column_22 = CStr(myreader2.GetString(21))
                    column_23 = CStr(myreader2.GetString(22))
                    oWrite2.WriteLine("{0}" & t & "{1}" & t & "{2}" & t & "{3}" & t & "{4}" & t & "{5}" & t & "{6}" & t & "{7}" & t & "{8}" & t & "{9}" & t & "{10}" & t & "{11}" & t & "{12}" & t & "{13}" & t & "{14}" & t & "{15}" & t & "{16}" & t & "{17}" & t & "{18}" & t & "{19}" & t & "{20}" & t & "{21}" & t & "{22}", column_1, column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23)
                End While

Open in new window


Any idea why I am getting this error?

T
0
Comment
Question by:thoecherl
8 Comments
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41884389
Try changing Convert.ToInt32(cmd.ExecuteScalar()) instead of  intCount = cmd.ExecuteScalar()...
0
 
LVL 19

Expert Comment

by:darbid73
ID: 41884469
oWrite2.WriteLine("{0}" & t & "{1}" & t & "{2}" & t & "{3}" & t & "{4}" & t & "{5}" & t & "{6}" & t & "{7}" & t & "{8}" & t & "{9}" & t & "{10}" & t & "{11}" & t & "{12}" & t & "{13}" & t & "{14}" & t & "{15}" & t & "{16}" & t & "{17}" & t & "{18}" & t & "{19}" & t & "{20}" & t & "{21}" & t & "{22}", column_1, column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23)

Open in new window


What is the "t" used for here?
0
 

Author Comment

by:thoecherl
ID: 41886588
Darbid73:

 Dim t As Char = vbTab
0
 
LVL 18

Expert Comment

by:Pawan Kumar Khowal
ID: 41887657
Hi Author,

Any update on this? Have u tried out the suggestion given above?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:thoecherl
ID: 41888163
Pawan Kumar Khowal,

Going to client site tomorrow to try your suggestion.  I will let you know the result.

T
0
 

Author Comment

by:thoecherl
ID: 41892088
Pawan Kumar Khowal,

I tried your suggestion.  I also tried this on the code two lines earlier:

cmd.CommandText = "EXECUTE udp_TEMPERED_PO '" & strBatch & "'"
             Convert.ToInt32(cmd.ExecuteNonQuery())  

Neither change eliminated the error.  That didn't surprise me, though because I commented out all of the code and then started uncommenting a line or two at a time.  When those two lines that we changed according to your suggestion were uncommented, the error didn't come back.  The lines that cause the error are these:

While myreader2.Read
                     column_1 = CStr(myreader2.GetString(0))
                     column_2 = CStr(myreader2.GetString(1))
                     column_3 = CStr(myreader2.GetString(2))
                     column_4 = CStr(myreader2.GetString(3))
                     column_5 = CStr(myreader2.GetString(4))
                     column_6 = CStr(myreader2.GetString(5))
                     column_7 = CStr(myreader2.GetString(6))
                     column_8 = CStr(myreader2.GetString(7))
                     column_9 = CStr(myreader2.GetString(8))
                     column_10 = CStr(myreader2.GetString(9))
                     column_11 = CStr(myreader2.GetString(10))
                     column_12 = CStr(myreader2.GetString(11))
                     column_13 = CStr(myreader2.GetString(12))
                     column_14 = CStr(myreader2.GetString(13))
                     column_15 = CStr(myreader2.GetString(14))
                     column_16 = CStr(myreader2.GetString(15))
                     column_17 = CStr(myreader2.GetString(16))
                     column_18 = CStr(myreader2.GetString(17))
                     column_19 = CStr(myreader2.GetString(18))
                     column_20 = CStr(myreader2.GetString(19))
                     column_21 = CStr(myreader2.GetString(20))
                     column_22 = CStr(myreader2.GetString(21))
                     column_23 = CStr(myreader2.GetString(22))
                     oWrite2.WriteLine("{0}" & t & "{1}" & t & "{2}" & t & "{3}" & t & "{4}" & t & "{5}" & t & "{6}" & t & "{7}" & t & "{8}" & t & "{9}" & t & "{10}" & t & "{11}" & t & "{12}" & t & "{13}" & t & "{14}" & t & "{15}" & t & "{16}" & t & "{17}" & t & "{18}" & t & "{19}" & t & "{20}" & t & "{21}" & t & "{22}", column_1, column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23)
                 End While

Open in new window


If they are commented out, the error does not occur (of course, my file doesn't get written, either).  When these lines are uncommented, the error occurs.

Any other ideas?

T
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 41892211
GetString is only used for string (varchar) columns. For other column types you should use GetInt32, GetDateTime etc. See docs here.

There is an easier way, just use:
column_1 = CStr(myreader2(0))

Open in new window

etc. but this is not the nicest way and you may run into trouble with NULL values.

So try this even easier way:
            Dim arrValues(myreader2.FieldCount - 1) As Object
            While myreader2.Read
                myreader2.GetValues(arrValues)
                oWrite2.WriteLine(String.Join(t, arrValues))
            End While

Open in new window

0
 

Author Closing Comment

by:thoecherl
ID: 41893721
Thank you
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

9 Experts available now in Live!

Get 1:1 Help Now