Solved

System32Int Error

Posted on 2016-11-11
8
56 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 24

Expert Comment

by:Pawan Kumar
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 24

Expert Comment

by:Pawan Kumar
ID: 41887657
Hi Author,

Any update on this? Have u tried out the suggestion given above?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

19 Experts available now in Live!

Get 1:1 Help Now