Solved

System32Int Error

Posted on 2016-11-11
8
62 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 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

785 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