none
Unable to cast object of type 'System.Int32' to type 'System.String'. RRS feed

  • Question

  •            iNCEEB = .First.NCEEB '<-- Im getting the error in this line, iNCEEB is integer .First.NCEEB is short.

    NCEEB Server Datatype property is smallint

    where is that "to type 'System.String'." coming from?

    I'm using vs2008 and mssql2008.

        Dim dbCEE As New CEE_LinqDataContext

        Dim iAppNo As Integer = 0

        Dim iNCEEB As Short? = Nothing
        Dim sNQI As String = ""
        Dim iRCEEB As Integer? = Nothing

    Private Sub SearchCEE_Results()

            'Search Scores for CEE
            iAppNo = Integer.Parse(iAppNo)
            Dim CEESearchByAppNo = From CEEsearch In dbCEE.tblScanneds _
                                         Where CEEsearch.AppNo = iAppNo

            With CEESearchByAppNo

                If .Count > 0 Then
                    iNCEEB = .First.NCEEB '<-- Im getting the error here
                    sNQI = .First.NQI
                    iRCEEB = .First.RCCEEB
                    sRQI = .First.RCQI
                    iICEEB = .First.ICEEB
                    sIQI = .First.ITQI
                    iTCEEB = .First.Total_CEEB
                    sTQI = .First.TotQI
                    sCEE_EXAMDATE = .First.ExamDate
                    iCEE_Count = .Count
                Else

    Wednesday, April 11, 2012 6:08 AM

Answers

  • what I did was this.

    Since it was working before, I didn't change my database structure and didn't change my code either went to LinqDataContext, reconnect to the database, delete the tblScanned and dropped same table using server explorer in the LinqDataContext. Now its working.

    my code:

     Dim iAppNo As Integer = 0
        'Dim iNCEEB As Short? = Nothing
        Dim iNCEEB As Integer? = Nothing
        Dim sNQI As String = ""
        Dim iRCEEB As Integer? = Nothing
        Dim sRQI As String = ""
        Dim iICEEB As Integer? = Nothing
        Dim sIQI As String = ""
        Dim iTCEEB As Integer? = Nothing
        Dim sTQI As String = ""
        Dim iEptCEEB As Integer? = Nothing
        Dim sEptQI As String = ""
        Dim sEPT_Examdate As String = ""
        Dim sCEE_EXAMDATE As String = ""
        Dim iCEE_Count As Integer
        Dim iEPT_Count As Integer

      Private Sub SearchCEE_Results()

            'Search Scores for CEE
            iAppNo = Integer.Parse(iAppNo)
            Dim CEESearchByAppNo = From CEEsearch In dbCEE.tblScanneds _
                                         Where CEEsearch.AppNo = iAppNo

            With CEESearchByAppNo
                If .Count > 0 Then
                    iNCEEB = .First.NCEEB
                    sNQI = .First.NQI
                    iRCEEB = .First.RCCEEB
                    sRQI = .First.RCQI
                    iICEEB = .First.ICEEB
                    sIQI = .First.ITQI
                    iTCEEB = .First.Total_CEEB
                    sTQI = .First.TotQI
                    sCEE_EXAMDATE = .First.ExamDate
                    iCEE_Count = .Count
                Else
                    iNCEEB = Nothing
                    sNQI = ""
                    iRCEEB = Nothing
                    sRQI = ""
                    iICEEB = Nothing
                    sIQI = ""
                    iTCEEB = Nothing
                    sTQI = ""
                    sCEE_EXAMDATE = DateTime.MinValue
                    iCEE_Count = 0
                End If
            End With

        End Sub

    • Marked as answer by WalangAlam Thursday, April 12, 2012 7:41 AM
    Thursday, April 12, 2012 1:41 AM

All replies

  • Hello WalangAlam,

               iNCEEB = .First.NCEEB '<-- Im getting the error in this line, iNCEEB is integer .First.NCEEB is short.

    NCEEB Server Datatype property is smallint

    where is that "to type 'System.String'." coming from?

    I'm using vs2008 and mssql2008.

        Dim dbCEE As New CEE_LinqDataContext

        Dim iAppNo As Integer = 0

        Dim iNCEEB As Short? = Nothing
        Dim sNQI As String = ""
        Dim iRCEEB As Integer? = Nothing

    Private Sub SearchCEE_Results()

            'Search Scores for CEE
            iAppNo = Integer.Parse(iAppNo)
            Dim CEESearchByAppNo = From CEEsearch In dbCEE.tblScanneds _
                                         Where CEEsearch.AppNo = iAppNo

            With CEESearchByAppNo

                If .Count > 0 Then
                    iNCEEB = .First.NCEEB '<-- Im getting the error here
                    sNQI = .First.NQI
                    iRCEEB = .First.RCCEEB
                    sRQI = .First.RCQI
                    iICEEB = .First.ICEEB
                    sIQI = .First.ITQI
                    iTCEEB = .First.Total_CEEB
                    sTQI = .First.TotQI
                    sCEE_EXAMDATE = .First.ExamDate
                    iCEE_Count = .Count
                Else

      I also seefrom the code thatthe variableDimiNCEEBand Short,
    declaring
    Dim iNCEEB As Integer =0

    Regards.


    Carmelo La Monica

    Wednesday, April 11, 2012 6:14 AM
  • Hello WalangAlam,

               iNCEEB = .First.NCEEB '<-- Im getting the error in this line, iNCEEB is integer .First.NCEEB is short.

    NCEEB Server Datatype property is smallint

    where is that "to type 'System.String'." coming from?

    I'm using vs2008 and mssql2008.

        Dim dbCEE As New CEE_LinqDataContext

        Dim iAppNo As Integer = 0

        Dim iNCEEB As Short? = Nothing
        Dim sNQI As String = ""
        Dim iRCEEB As Integer? = Nothing

    Private Sub SearchCEE_Results()

            'Search Scores for CEE
            iAppNo = Integer.Parse(iAppNo)
            Dim CEESearchByAppNo = From CEEsearch In dbCEE.tblScanneds _
                                         Where CEEsearch.AppNo = iAppNo

            With CEESearchByAppNo

                If .Count > 0 Then
                    iNCEEB = .First.NCEEB '<-- Im getting the error here
                    sNQI = .First.NQI
                    iRCEEB = .First.RCCEEB
                    sRQI = .First.RCQI
                    iICEEB = .First.ICEEB
                    sIQI = .First.ITQI
                    iTCEEB = .First.Total_CEEB
                    sTQI = .First.TotQI
                    sCEE_EXAMDATE = .First.ExamDate
                    iCEE_Count = .Count
                Else

      I also seefrom the code thatthe variableDimiNCEEBand Short,
    declaring
    Dim iNCEEB As Integer =0

    Regards.


    Carmelo La Monica

    • Edited by Brent Serbus Wednesday, May 23, 2012 8:14 PM html issue
    Wednesday, April 11, 2012 6:53 AM
  • Hi,

    In line iNCEEB = .First.NCEEB

    Try:

    if isDBNull(.First.NCEEB) then
       inCEEB = 0
    else
       inCEEB = Convert.ToShort(.First.NCEEB)
    end if
    

    Maybe that .First.NCEEB has nothing value.

    Regards


    Agung Gugiaji My application developer journals

    Wednesday, April 11, 2012 7:00 AM
  • if isDBNull(.First.NCEEB) then <-- I'm getting same error here
    inCEEB = 0
    else
    inCEEB = Convert.ToShort(.First.NCEEB)
    end if

    I also tried to show .First.NCEEB in a messagebox but same error.

    like so

     With CEESearchByAppNo
                If .Count > 0 Then
                    MessageBox.Show(.First.NCEEB)<--- same error here already.
                    iNCEEB = .First.NCEEB

    • Edited by WalangAlam Wednesday, April 11, 2012 7:16 AM
    Wednesday, April 11, 2012 7:14 AM
  • Walang you cannot "cast" those. Be aware the word cast is often misused. 

    You can cast a class to its base class or cast it using the interface which is implemented. 

    That is for Int32 and String not available. 

    However you can convert an int to string

    Dim x As String = CStr(0) 

    if you want to put the decimal byte value of 0 in a string then it is

    Dim x  = CStr(Asc(CStr(0)))

    Also be aware that the use of int16 means less performance and not any memory gain, because the lowest integer which can be stored on a 32 bit system is Int32 and on a 64 bit system Int64.

    The conversion between integers from DataBase to .Net and vice verse is done by the System.Data namespace automatically.


    Success
    Cor


    Wednesday, April 11, 2012 8:01 AM
  • There's nothing here to indicate that NCEEB is a Short data type. Hover over it when you break on that line, and it'll tell you the type.

    (Ah, it's possible the data type has been Inferred since you are using LINQ, it seems).


    Stephen J Whiteley

    Wednesday, April 11, 2012 2:14 PM
    Moderator
  • what I did was this.

    Since it was working before, I didn't change my database structure and didn't change my code either went to LinqDataContext, reconnect to the database, delete the tblScanned and dropped same table using server explorer in the LinqDataContext. Now its working.

    my code:

     Dim iAppNo As Integer = 0
        'Dim iNCEEB As Short? = Nothing
        Dim iNCEEB As Integer? = Nothing
        Dim sNQI As String = ""
        Dim iRCEEB As Integer? = Nothing
        Dim sRQI As String = ""
        Dim iICEEB As Integer? = Nothing
        Dim sIQI As String = ""
        Dim iTCEEB As Integer? = Nothing
        Dim sTQI As String = ""
        Dim iEptCEEB As Integer? = Nothing
        Dim sEptQI As String = ""
        Dim sEPT_Examdate As String = ""
        Dim sCEE_EXAMDATE As String = ""
        Dim iCEE_Count As Integer
        Dim iEPT_Count As Integer

      Private Sub SearchCEE_Results()

            'Search Scores for CEE
            iAppNo = Integer.Parse(iAppNo)
            Dim CEESearchByAppNo = From CEEsearch In dbCEE.tblScanneds _
                                         Where CEEsearch.AppNo = iAppNo

            With CEESearchByAppNo
                If .Count > 0 Then
                    iNCEEB = .First.NCEEB
                    sNQI = .First.NQI
                    iRCEEB = .First.RCCEEB
                    sRQI = .First.RCQI
                    iICEEB = .First.ICEEB
                    sIQI = .First.ITQI
                    iTCEEB = .First.Total_CEEB
                    sTQI = .First.TotQI
                    sCEE_EXAMDATE = .First.ExamDate
                    iCEE_Count = .Count
                Else
                    iNCEEB = Nothing
                    sNQI = ""
                    iRCEEB = Nothing
                    sRQI = ""
                    iICEEB = Nothing
                    sIQI = ""
                    iTCEEB = Nothing
                    sTQI = ""
                    sCEE_EXAMDATE = DateTime.MinValue
                    iCEE_Count = 0
                End If
            End With

        End Sub

    • Marked as answer by WalangAlam Thursday, April 12, 2012 7:41 AM
    Thursday, April 12, 2012 1:41 AM
  • Walang, 

    Sorry, I did not see because of the thrash below this was code from used with a generated datacontext.

    That has hidden designer code on board.

    You can see it by doing in top of Solution Explorer "show all files".

    Maybe you had an old generated designer part which contained wrong type names for the created properties and fields.



    Success
    Cor


    • Edited by Cor Ligthert Thursday, April 12, 2012 5:22 AM typo
    Thursday, April 12, 2012 5:22 AM