Solved

System32Int Error

Posted on 2016-11-11
8
69 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 28

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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

840 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