Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

System32Int Error

Posted on 2016-11-11
8
Medium Priority
?
108 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
[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
8 Comments
 
LVL 32

Expert Comment

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

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41887657
Hi Author,

Any update on this? Have u tried out the suggestion given above?
0
 

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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

596 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