VS 2017 [RESOLVED] System.InvalidCastException-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 61

Thread: [RESOLVED] System.InvalidCastException

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Resolved [RESOLVED] System.InvalidCastException

    I have been trying to understand what is going on here. I know that .Current is an object, but beyond that I have no idea why this exception is thrown.

    Name:  InvalidCast.jpg
Views: 127
Size:  12.8 KB

  2. #2
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    That can't be seen, try upload the image to https://imgur.com

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Ah laddie, you need to get new glasses. I can almost read it without mine (not quite, but almost). Anyway, before I sign up for something and go through a process let's try this. If this is still too small, I will make the effort the other way.

    Code:
                Case = 2
                    Me.tblChangeTrainBindingSource.AddNew()
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
                    dgvEmployees.Enabled = False
                    Me.Validate()
                    Me.tblChangeTrainBindingSource.EndEdit()
                    Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                    Me.Close()
    Name:  ErrorMessage.jpg
Views: 35
Size:  41.6 KB

  4. #4
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    You can replace the CType by DirectCast, alow i think you will get the same exception...
    You will need to post the rest of the code, i have no ideia of whats happing based just on that...
    Maybe someone else can.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Yeah, I get the same error. So let me post the code leading up to that:

    This code is from the change request form and Case = 7 is what is used in this case (pun intended)

    Code:
        Private Sub btnTraining_Click(sender As Object, e As EventArgs) Handles btnTraining.Click
            'Display list of trainees for a change request
            glbstrRevision = CStr(txtRev.Text)
            glbstrTitle = CStr(txtTitle.Text)
            glbintCount = 2
            DisableWindow()
            SubSignList.Show()
            Select Case glbIntForm
                Case = 5
                    DisableWindow()
                    SubSignList.Show()
                Case = 7
                    DisableWindow()
                    frmSignRecord.Show()
                Case = 8
                    DisableWindow()
                    frmSignRecord.Show()
                Case = 10
                    DisableWindow()
                    frmSignRecord.Show()
                Case Else
                    _strError = "frmChangeRequest, btnTraining() event"
                    CaseExceptionCode1003()
            End Select
        End Sub
    I don't believe that this has anything to do with anything, but that was called in the code above. It renders the change request form inactive and makes the form somewhat transparent.

    Code:
        Private Sub DisableWindow()
            'Reduce opacity of CR form and disable.
            With frmChangeRequest.ActiveForm
                .Opacity = 0.5
                .Enabled = False
            End With
        End Sub
    This is from the load event of frmSignRecord form. Case glbintCount = 2
    Code:
                Case 7
                    'New Item/part/component
                    Select Case glbintCount
                        Case = 1
                            'Approval
                            Me.LnkChangeRequestTableAdapter.FillByChangeNum(Me._MasterBase4_0ItemMasterDataSet.lnkChangeRequest, glbintCRNum)
                            ApproveDisplayProperties()
                            ApproverList()
                        Case = 2
                            'Training
                            Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
                            TrainDisplayProperties()
                            TraineeList()
                        Case Else
                            _StrError = "ItemMaster 4.0, frmSignRecord.vb, frmSignRecord_Load() Event, Select Case glbintCount"
                            CaseExceptionCode1003()
                    End Select
    TrainDisplayProperties(), I did not include. All it does is define some of the form control properties for this particular usage of the form

    This routine, called in the load event, defines what is displayed in the DGV on the form

    Code:
        Private Sub TraineeList()
            Try
                Me.TblChangeTrainTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeTrain, glbintCRNum)
                dgvSignList.DataSource = Me._MasterBase4_0ItemMasterDataSet
                dgvSignList.Height = 406
                dgvSignList.Width = 779
                dgvSignList.DataMember = "TblChangeTrain"
                dgvSignList.Columns(0).Visible = False
                dgvSignList.Columns(1).Visible = False
                dgvSignList.Columns(2).HeaderText = "Name"
                dgvSignList.Columns(2).Width = 275
                dgvSignList.Columns(3).HeaderText = "Job Title"
                dgvSignList.Columns(3).Width = 275
                dgvSignList.Columns(4).HeaderText = "Department"
                dgvSignList.Columns(4).Width = 225
                dgvSignList.Columns(5).Visible = False
                dgvSignList.Columns(6).Visible = False
                dgvSignList.Columns(7).Visible = False
                dgvSignList.Columns(8).Visible = False
                dgvSignList.Columns(9).Visible = False
            Catch ex As Exception
                _StrError = "ItemMaster 4.0, frmSignRecord.vb, DisplayTrainees() Routine"
                QueryCallErrorCode1002()
            End Try
        End Sub
    This is where the work begins. I click the add button. Case = 2 is what is relevant
    Yeah, I can see that approval is spelled wrong, but no one is perfect. I will get to that eventually.

    Code:
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
            Dim newAproval As New subEmployeeList
            newAproval.ShowDialog()
            Select Case glbintCount
                Case = 1
                    Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
                Case = 2
                    Me.TblChangeTrainTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeTrain, glbintCRNum)
                Case Else
                    _StrError = "ItemMaster 4.0, frmSignRecord.vb, btnAdd_Click() Event, Select Case glbintCount"
                    CaseExceptionCode1003()
            End Select
        End Sub
    This is the load event from subEmployeeList. Case = 2 is relevant

    Code:
        Private Sub subEmployeeList_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Select Case glbintCount
                Case = 1
                    'Approver selection
                    Me.LkpDepartmentTableAdapter.Fill(Me._MasterBase4_0ItemMasterDataSet.lkpDepartment)
                    cmbDepartment.SelectedIndex = -1
                    With lblInfo
                        .Visible = True
                        .TextAlign = ContentAlignment.MiddleLeft
                        .Text = String.Format("Select a department using the ComboBox above.{0}From the generated list below, double click in a row and that person will be added to the Approver list",
                                              vbNewLine)
                    End With
                    With radIndividual
                        .Visible = False
                    End With
                    With RadDepartment
                        .Visible = False
                    End With
                Case = 2
                    'Trainee selection
                    Me.LkpDepartmentTableAdapter.Fill(Me._MasterBase4_0ItemMasterDataSet.lkpDepartment)
                    With cmbDepartment
                        .SelectedIndex = -1
                        .Visible = False
                    End With
                    With lblInfo
                        .Visible = True
                        .TextAlign = ContentAlignment.MiddleLeft
                        .Text = String.Format("Click in the By Department Radial button to select a group of trainees.{0}Click in the By Individual to select only one trainee.",
                                              vbNewLine)
                    End With
                    With radIndividual
                        .Visible = True
                    End With
                    With RadDepartment
                        .Visible = True
                    End With
                Case Else
                    _StrError = "ItemMaster 4.0, frmSignRecord.vb, subEmployeeList_Load() Event"
                    CaseExceptionCode1003()
            End Select
        End Sub
    This is the form that is used for both approvers and trainees. The difference is that trainees can be selected as a group (by department) or as individuals. So the two radio buttons are only visible for trainees.

    Name:  EmployeeListOne.jpg
Views: 34
Size:  16.0 KB

    So right now I am working to add a single trainee and click the radio button for individuals. All it does is makes the department combobox visible and defines the value of a variable.

    Code:
        Private Sub radIndividual_CheckedChanged(sender As Object, e As EventArgs) Handles radIndividual.CheckedChanged
            With lblInfo
                .Visible = True
                .TextAlign = ContentAlignment.MiddleLeft
                .Text = String.Format("Select a department using the Combobox.{0}Double Click in the row of any individual to be added to the Trainee List.",
                                      vbNewLine)
            End With
            cmbDepartment.Visible = True
            _intTrain = 2
        End Sub
    Because I am only adding an individual to tblChangeTrain, Case = 2 and _intTrain = 2 (in the If branch) is relevant.

    Code:
        Private Sub cmbDepartment_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbDepartment.SelectedIndexChanged
            Select Case glbintCount
                Case = 1
                    glbstrDept = cmbDepartment.Text
                    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                Case = 2
                    glbstrDept = cmbDepartment.Text
                    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                    If _intTrain = 1 Then
                        AddTrainee()
                    ElseIf _intTrain = 2 Then
                        With lblInfo
                            .Visible = True
                            .TextAlign = ContentAlignment.MiddleLeft
                            .Text = String.Format("From the list below, double click{0}a name in the list to add them to the Approver list", vbNewLine)
                        End With
                    End If
                Case Else
                    _StrError = "ItemMaster 4.0, frmSignRecord.vb, cmbDepartment_SelectedIndexChanged() Event, Select Case glbintCount"
                    CaseExceptionCode1003()
            End Select
        End Sub
    Since this is an individual, I then double click in a row of the DGV to select the individual and carry out the data transfer.
    The DGV would not be enabled to do this had I selected to move a group of trainees to tblChangeTrain.
    Case = 2 is relevant.

    Code:
        Private Sub dgvEmployees_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployees.CellDoubleClick
            Select Case glbintCount
                Case = 1
                    AddApprover()
                Case = 2
                    AddTrainee()
                Case Else
                    _StrError = "ItemMaster 4.0, frmSignRecord.vb, DisplayApprovers() Routine"
                    QueryCallErrorCode1002()
            End Select
        End Sub
    We have reached the problem child. Case = 2 is relevant.
    Case = 1 has not yet been worked to match how I want to do this now, but is what I was previously doing for moving a group of trainees to tblChangeTrain.
    So anyway, here is how I got here and where the error occurs.
    Code:
        Private Sub AddTrainee()
            Select Case _intTrain
                Case = 1
                    'Add group of trainee, filtered by department to training table.
                    glbstrDept = CStr(cmbDepartment.SelectedValue)
                    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                    'dgvEmployees.Visible = True
                    Dim intRowCount As Integer = 0
                    Dim intCountRow As Integer = 0
                    'glbintCRNum = CInt(lblCRNum.Text)
                    glbstrDept = CStr(Me.cmbDepartment.SelectedValue)
                    'Filter the user table to include users from a specified department, then count the number of filtered users and return that number
                    intRowCount = CInt(If(Me.TblEmployeeTableAdapter.qryCountRow(glbstrDept), 0))
                    Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                    Do Until intCountRow >= intRowCount
                        'Get data from user table row
                        glbstrName = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strFullName
                        glbstrPassword = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strPassword
                        glbstrLogin = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strLogin
                        glbstrJob = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strJobTitle
                        'Send one row of data and add to training list
                        Dim NewTrainingRow As _MasterBase4_0ItemMasterDataSet.tblChangeTrainRow
                        NewTrainingRow = _MasterBase4_0ItemMasterDataSet.tblChangeTrain.NewtblChangeTrainRow()
                        NewTrainingRow.intChangeID = glbintCRNum
                        NewTrainingRow.StrName = glbstrName
                        NewTrainingRow.strJobTitle = glbstrJob
                        NewTrainingRow.strDept = glbstrDept
                        NewTrainingRow.strLogin = glbstrLogin
                        NewTrainingRow.strPassword = glbstrPassword
                        NewTrainingRow.strMasterBaseType = glbstrBaseCategory
                        NewTrainingRow.blnRemoved = False
                        _MasterBase4_0ItemMasterDataSet.tblChangeTrain.Rows.Add(NewTrainingRow)
                        intCountRow += 1
                    Loop
                    Me.Validate()
                    Me.tblChangeTrainBindingSource.EndEdit()
                    Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                Case = 2
                    Me.tblChangeTrainBindingSource.AddNew()
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
                    dgvEmployees.Enabled = False
                    Me.Validate()
                    Me.tblChangeTrainBindingSource.EndEdit()
                    Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                    Me.Close()
                Case Else
            End Select
        End Sub

  6. #6
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Try comment that line that gives you a error and see if the other ones, execute.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    I actually already did that. I went down about three of them then quit. All of them produced the same error.

    I also ran this for approvers instead of trainees, which uses this code to transfer the data:

    Code:
            Me.tblChangeApproveBindingSource.AddNew()
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
            dgvEmployees.Enabled = False
            Me.Validate()
            Me.tblChangeApproveBindingSource.EndEdit()
            Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
            Me.Close()
    It executes without error. So it is obvious to me that there is something wrong, but where that occurs flummoxes me (I am not sure what a flummox is, but you know what I mean).

  8. #8
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Add the Try catch like this to see what the exception message returns:

    Code:
     Private Sub AddTrainee()
    
            Try
    
                Select Case _intTrain
                    Case = 1
                        'Add group of trainee, filtered by department to training table.
                        glbstrDept = CStr(cmbDepartment.SelectedValue)
                        Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                        'dgvEmployees.Visible = True
                        Dim intRowCount As Integer = 0
                        Dim intCountRow As Integer = 0
                        'glbintCRNum = CInt(lblCRNum.Text)
                        glbstrDept = CStr(Me.cmbDepartment.SelectedValue)
                        'Filter the user table to include users from a specified department, then count the number of filtered users and return that number
                        intRowCount = CInt(If(Me.TblEmployeeTableAdapter.qryCountRow(glbstrDept), 0))
                        Me.TblEmployeeTableAdapter.FillByDepartment(Me._MasterBase4_0ItemMasterDataSet.tblEmployee, glbstrDept)
                        Do Until intCountRow >= intRowCount
                            'Get data from user table row
                            glbstrName = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strFullName
                            glbstrPassword = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strPassword
                            glbstrLogin = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strLogin
                            glbstrJob = _MasterBase4_0ItemMasterDataSet.tblEmployee(intCountRow).strJobTitle
                            'Send one row of data and add to training list
                            Dim NewTrainingRow As _MasterBase4_0ItemMasterDataSet.tblChangeTrainRow
                            NewTrainingRow = _MasterBase4_0ItemMasterDataSet.tblChangeTrain.NewtblChangeTrainRow()
                            NewTrainingRow.intChangeID = glbintCRNum
                            NewTrainingRow.StrName = glbstrName
                            NewTrainingRow.strJobTitle = glbstrJob
                            NewTrainingRow.strDept = glbstrDept
                            NewTrainingRow.strLogin = glbstrLogin
                            NewTrainingRow.strPassword = glbstrPassword
                            NewTrainingRow.strMasterBaseType = glbstrBaseCategory
                            NewTrainingRow.blnRemoved = False
                            _MasterBase4_0ItemMasterDataSet.tblChangeTrain.Rows.Add(NewTrainingRow)
                            intCountRow += 1
                        Loop
                        Me.Validate()
                        Me.tblChangeTrainBindingSource.EndEdit()
                        Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                    Case = 2
                        Me.tblChangeTrainBindingSource.AddNew()
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
                        dgvEmployees.Enabled = False
                        Me.Validate()
                        Me.tblChangeTrainBindingSource.EndEdit()
                        Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                        Me.Close()
                    Case Else
                End Select
    
            Catch ex As Exception
                MessageBox.Show(String.Format("Message: {0} {1} StackTrace: {2}", ex.Message, vbNewLine, ex.StackTrace))
            End Try
    
        End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    OK, Ran it. Here is the error message:

    Name:  ErrorMessage.jpg
Views: 31
Size:  33.0 KB

    Here is the line referenced:

    Code:
                        CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
    By the way, thanks for that by itself. I have been trying to figure out for awhile how to reference a specific line for my error messages.

  10. #10
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    You welcome, i had told you before to wrap your code in Try catch i think.
    As for that, i can't figure the error, with out testing the code in my machine, what cant do now.
    In your case i would go tro all the code paying attenction to all does Select Case to garanti that all the tables have been Initiated proply.

    As a observation, something that i already asked you before, the way you proceed seems to me that bouth bindingsources are in the same form, is that correct?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Yeah, that is what it looks like to me too. I don't think the problem is from the transfer routine, but somewhere upstream in the code. I have to say I hate that because I have a little dyslexia and it really takes me awhile to find tickey thing like that. Also, you did tell me about the wrap code and I am slowly going through all of my messages and making that change.

    Thanks anyway Mike.

  12. #12
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    As a guess, couse i cant process all that code in my head, is that the table that tblChangeTrainBindingSource is binded too may not be fill.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Actually, I had thought of that too. So I ran it using a different method that I have used before and get the expected results. It uses the same fill query.

    Code:
                    Dim NewTrainingRow As _MasterBase4_0ItemMasterDataSet.tblChangeTrainRow
                    NewTrainingRow = _MasterBase4_0ItemMasterDataSet.tblChangeTrain.NewtblChangeTrainRow()
                    NewTrainingRow.intChangeID = glbintCRNum
                    NewTrainingRow.StrName = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strFullName
                    NewTrainingRow.strJobTitle = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strJobTitle
                    NewTrainingRow.strDept = glbstrDept
                    NewTrainingRow.strLogin = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strLogin
                    NewTrainingRow.strPassword = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strPassword
                    NewTrainingRow.strMasterBaseType = glbstrBaseCategory
                    NewTrainingRow.blnRemoved = False
                    _MasterBase4_0ItemMasterDataSet.tblChangeTrain.Rows.Add(NewTrainingRow)
                    dgvEmployees.Enabled = False
                    Me.Validate()
                    Me.tblChangeTrainBindingSource.EndEdit()
                    Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
                    Me.Close()

  14. #14
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Lets see what others have to say, i have done what i can.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Again, thanks a lot Mike. This evening I am going to slog through all of the code and see where the problem occurs. I believe that it is upstream somewhere and something subtle.

  16. #16
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    It just come to me, where do you set the value to glbintCRNum

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    That is set back in the change request form. Since this is a new change request a new ChangeID has to be generated. I like to be able to control some of my ID numbers, so what I do is where I want to do this I have a table that has one row and one field. The field, in this case, is an integer field which I set the beginning number and it works like this:

    Code:
        Private Sub NewChangeID()
            'Create new Change Request number
            Try
                Me.LkpChangeIDTableAdapter.Fill(Me._MasterBase4_0ItemMasterDataSet.lkpChangeID)
                glbintCRNum = Me._MasterBase4_0ItemMasterDataSet.lkpChangeID(0).intChangeID
                glbintCRNum += 1
            Catch ex As Exception
                _strError = "frmChangeRequest, NewChangeID() Routine"
                QueryCallErrorCode1002()
            End Try
        End Sub
    This also contains one of the error messages that I have yet to get around to going to the Format.String method. When I save the change request the new number will replace the old in the table.

    So I will step through the code and see if and where the value for glbintChangeNum might occur.

    Thanks

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    I just stepped through the code and at the point of error, the valud for glbintCRNum is what was expected.

  19. #19
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Is your database SQL or access?

    You do know that if you working in a network, or plan to make that app run on a network that last id genarator will be a issue?

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    DB is access. I would like to go to SQL, but access is free and SQL isn't.
    Having said that, I eventually will get to that point where I want to put this on a network. I have also considered Oracle and SAP, but I do not like either of those DBs. But at this point I don't even begin to have the skills or knowledge to do so anytime real soon.

  21. #21
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    SQL EXPRESS is free and it handles a database up to 10GB on 2012/2014/2016 versions.

    2012
    https://www.microsoft.com/en-us/down....aspx?id=50003

    2014
    https://www.microsoft.com/en-us/down....aspx?id=42299

    2016
    https://www.microsoft.com/pt-pt/sql-...itions-express

    So you can take a pick on the version that suits the system you want to run it.
    Last edited by Mike Storm; Oct 1st, 2017 at 04:50 PM.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Thanks Mike, the price is right. I will take a look at it. Can I assume a lot of the problems I am having would go away if I was using SQL? Do I still use table adapters and databindings?

  23. #23
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    The problems you having now, will still be there i think, couse they code related, yes you will be using datasets, table adapters etc as u do now, what will change is you can use SQLClient insted of OleDB if it is that you using now.
    In performance and programability od the database, Access is a toy compared to SQL. As for working in network access is messy, SQL is made for it.

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    I am in the process of downloading. With 10GB it is obvious that if I ever get to the point that I am actually running all of the applications I will have to shed some money, since 10 GB can run out pretty fast with data. Thanks man.

  25. #25
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Either you migrate to a payed version, or you use more then one database, i would probably migrate to the payed version, but that all depends on what you storing, will be storing files in it?

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    By the way, how much are we talking here if I make this move seriously? I would like to store files on/in it. Currently what I do is I have a routine that creates directories for files I want to keep and then I keep the directory paths in a field in the DB. It works well, but having them actually in the DB would be even better.

  27. #27
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    I will sugest you to open a new threead with that subject and let someone else answer to that question, i dont store files in my databases except for company logo and maybe more 2 or 3 cases and i do limit the size of the files that can be stored. I dont have the knowlege to answer to that alow i have a impression that to store files MySql maybe a better option from what i have been reading online.

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    I will do that. However, I won't be making any changes until I can workout all of the issues I currently have and see exactly what it is that the application really needs, beyond what I have already determined is required.

  29. #29
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Well, if you do plan to move from one DB to the other, i sugest you would do it sonner rather then later. Its my opinion.

  30. #30

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Probably so, but I have never been one to do anything the easy way or the best way. At this late stage of my life I want to move out of character very far. But I do understand what you are saying and generally agree with that.

  31. #31
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    Are you using Jet or OleDb to connect to your db?

  32. #32

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Using OleDb. I have never actually looked at Jet or what I get from that. I also admit that I am taking a look at SQL, even as we speak.

  33. #33
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,798

    Re: System.InvalidCastException

    This is a very strange error, and I don't know that I can help other than to explain what VB is telling you and why I'm confused.

    InvalidCastException means you tried to cast from one type to another type, but VB doesn't have a way to do that. For some types, like Integer and Double, VB knows it can do the conversion if it asks for it. For other types, like say, MessageBox to Integer, there's no sensible conversion so VB fails if you try to make that conversion. The most common reason people get InvalidCastException tends to be a String to Integer conversion. The message tells you "what it is" and "what you tried to convert it to", and usually that's a help.

    The thing that is confusing this veteran is the "what it is" is System.Object. That's very strange. Allow me to take a slight side trip to show you why that is.

    You can easily make Object-type variables. Since it's the implicit base class for all .NET objects, you can put anything in an Object-type variable:
    Code:
    Dim weird As Object
    weird = 10
    
    Dim weirder As Object
    weirder = New Form1()
    Most DataTable-related variables are of type Object, because it was designed way back in .NET 1.0 before we had generic types. So it's not strange that some random property of a data table might be of type Object. But "the variable is type Object" and "the variable contains an Object" are two different things. If I try this:
    Code:
    Dim weirder As Object
    weirder = New Form1()
    Dim broken As Integer = CType(weirder, Integer)
    I expect an InvalidCastException to tell me it can't cast from 'System.Windows.Forms.Form' to 'System.Int32'. That's because even though the "weirder" variable is of type Object, when the cast is made VB knows it's a Form. Then it has to figure out if it can cast Form to Integer, and it can't.

    So that's the weirdness. I'm looking at post #9. I'm curious what 'Me.tblChangeTrainBindingSource.Current' is. Actually, first I'm curious what 'Me.tblChangeTrainBindingSource' is. I'm assuming it's System.Windows.Forms.BindingSource. That has a 'Current' property and makes sense. You should verify this with a debugger.

    But what the exception is telling me is that property's value IS a System.Object. That's very strange. You can only really get a System.Object by creating one. It's an object with no properties. It's not a String or an Integer or a Double or anything like that. It's 'Object'. And that's really weird.

    Did this code ever work? If so, what did you change? That is more likely the cause than "I need to change my database".
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  34. #34

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Sitten,

    Everything you say is true. However, I am not really sure what applies here or even why this error is occurring (it has happened to me once before and instead of trying to find it I just used a different methodology).

    So let me review the process with you and see if you can see something I haven't.

    1. To begin with there are three forms involved in this and there are three tables as well.
    2. The first form is a change request form (seen below). In this case I am generating a new change request for a new part/material/item/whatever.

    Name:  ChangeRequest.jpg
Views: 19
Size:  41.6 KB

    As can be seen, a new change request only has a limited amount of data that can be entered. The only really important information, at this point, is the Change ID and the Item/Supplier ID.

    In this application a change request must be approved and might well require some people to be trained. There are two buttons, relate to that. The Approver CR button and the Training button.

    By clicking the Approver button the following code is executed. Case = 7 is the relevant code. The call to DisableWindow() code is not included, but all it does is make the change request form inactive and translucent. So the result is the opening of the next form.

    Code:
        Private Sub btnApprove_Click(sender As Object, e As EventArgs) Handles btnApprove.Click
            'Display list of approvers for change request
            glbstrRevision = CStr(txtRev.Text)
            glbstrTitle = CStr(txtTitle.Text)
            glbintCount = 1
            Select Case glbIntForm
                Case = 5
                    DisableWindow()
                    SubSignList.Show()
                Case = 7
                    DisableWindow()
                    frmSignRecord.Show()
                Case = 8
                    DisableWindow()
                    frmSignRecord.Show()
                Case = 10
                    DisableWindow()
                    frmSignRecord.Show()
                Case Else
                    _strError = "frmChangeRequest, btnApprove() event"
                    CaseExceptionCode1003()
            End Select
        End Sub
    Name:  frmSignRecord.jpg
Views: 19
Size:  23.2 KB

    The relevant code from the load event of the form.

    Code:
                    'New Item/part/component
                    Select Case glbintCount
                        Case = 1
                            'Approval
                            Me.LnkChangeRequestTableAdapter.FillByChangeNum(Me._MasterBase4_0ItemMasterDataSet.lnkChangeRequest, glbintCRNum)
                            ApproveDisplayProperties()
                            ApproverList()
                        Case = 2
                            'Training
                            Me.LnkChangeRequestTableAdapter.FillByChangeNum(Me._MasterBase4_0ItemMasterDataSet.lnkChangeRequest, glbintCRNum)
                            TrainDisplayProperties()
                            TraineeList()
                        Case Else
                            _StrError = "ItemMaster 4.0, frmSignRecord.vb, frmSignRecord_Load() Event, Select Case glbintCount"
                            CaseExceptionCode1003()
                    End Select
    Based on the fact that I clicked the Approver form in the change request form, Case = 1 applies. ApproveDisplayProperties() does nothing more than set control properties in the form and I do not believe to be relevant to the issue so is not included.

    The DGV has no databindings or members and those are set based on whether I am generating approvers to tblChangeApprove or trainees to tblChangeTrain. In this case it is approvers.

    Code:
        Private Sub ApproverList()
            Try
                Me.TblChangeApproveTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeApprove, glbintCRNum)
                dgvSignList.DataSource = Me._MasterBase4_0ItemMasterDataSet
                dgvSignList.Height = 406
                dgvSignList.Width = 779
                dgvSignList.DataMember = "TblChangeApprove"
                dgvSignList.Columns(0).Visible = False
                dgvSignList.Columns(1).Visible = False
                dgvSignList.Columns(2).HeaderText = "Name"
                dgvSignList.Columns(2).Width = 275
                dgvSignList.Columns(3).HeaderText = "Job Title"
                dgvSignList.Columns(3).Width = 275
                dgvSignList.Columns(4).HeaderText = "Department"
                dgvSignList.Columns(4).Width = 225
                dgvSignList.Columns(5).Visible = False
                dgvSignList.Columns(6).Visible = False
                dgvSignList.Columns(7).Visible = False
                dgvSignList.Columns(8).Visible = False
                dgvSignList.Columns(9).Visible = False
            Catch ex As Exception
                _StrError = "ItemMaster 4.0, frmSignRecord.vb, DisplayApprovers() Routine"
                QueryCallErrorCode1002()
            End Try
        End Sub
    Since this is a new change request there have yet to be any approvers assigned so the DGV list is empty.

    At this point I will add an approver, which necessitates clicking the Add Approver(s) button, the generating another form.

    Name:  EmployeeTableONe.jpg
Views: 19
Size:  18.2 KB

    The DGV in this table is data bound to a table of employees and by selecting a department from the combobox it lists the employees from the selected department.

    Name:  EmployeesPartDuex.jpg
Views: 19
Size:  23.3 KB

    I did not include any of the code for this as I can see no relevance of it to the process.

    At this point I then select one of the employees to be added to the approvers table for this change request by double clicking in the row of the desired approver.

    Code:
        Private Sub dgvEmployees_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvEmployees.CellDoubleClick
            Select Case glbintCount
                Case = 1
                    AddApprover()
                Case = 2
                    AddTrainee()
                Case Else
                    _StrError = "ItemMaster 4.0, frmSignRecord.vb, DisplayApprovers() Routine"
                    QueryCallErrorCode1002()
            End Select
        End Sub
    Case = 1 is relevant

    Code:
        Private Sub AddApprover()
            Me.tblChangeApproveBindingSource.AddNew()
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
            CType(Me.tblChangeApproveBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
            dgvEmployees.Enabled = False
            Me.Validate()
            Me.tblChangeApproveBindingSource.EndEdit()
            Me.TblChangeApproveTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
            Me.Close()
        End Sub
    Name:  SignApprover.jpg
Views: 19
Size:  25.7 KB

    So as can be seen, the previous form has closed and we have returned to the approver form (same form used for trainees) and the selected approver shows in the DGV.

    The user would now close this window, unless they wanted to add more approvers. In which they would repeat the process of adding additional approvers.

    Now the user might want to add some people who require training on the subject of the change request, so he/she clicks the Training button. I did not include the code for this, since it is identical to the code for the Approver button.

    As before (see above) the same form is opened. However, since Case = 2 in the load event, the routine TraineeList() is called, which assigns the DGV the same DataSource, but a different table as the DataMember. Both tables are structured identically.

    Code:
                    Select Case glbintCount
                        Case = 1
                            'Approval
                            Me.LnkChangeRequestTableAdapter.FillByChangeNum(Me._MasterBase4_0ItemMasterDataSet.lnkChangeRequest, glbintCRNum)
                            ApproveDisplayProperties()
                            ApproverList()
                        Case = 2
                            'Training
                            Me.LnkChangeRequestTableAdapter.FillByChangeNum(Me._MasterBase4_0ItemMasterDataSet.lnkChangeRequest, glbintCRNum)
                            TrainDisplayProperties()
                            TraineeList()
    Code:
        Private Sub TraineeList()
            Try
                Me.TblChangeTrainTableAdapter.FillByChangeID(Me._MasterBase4_0ItemMasterDataSet.tblChangeTrain, glbintCRNum)
                dgvSignList.DataSource = Me._MasterBase4_0ItemMasterDataSet
                dgvSignList.Height = 406
                dgvSignList.Width = 779
                dgvSignList.DataMember = "TblChangeTrain"
                dgvSignList.Columns(0).Visible = False
                dgvSignList.Columns(1).Visible = False
                dgvSignList.Columns(2).HeaderText = "Name"
                dgvSignList.Columns(2).Width = 275
                dgvSignList.Columns(3).HeaderText = "Job Title"
                dgvSignList.Columns(3).Width = 275
                dgvSignList.Columns(4).HeaderText = "Department"
                dgvSignList.Columns(4).Width = 225
                dgvSignList.Columns(5).Visible = False
                dgvSignList.Columns(6).Visible = False
                dgvSignList.Columns(7).Visible = False
                dgvSignList.Columns(8).Visible = False
                dgvSignList.Columns(9).Visible = False
            Catch ex As Exception
                _StrError = "ItemMaster 4.0, frmSignRecord.vb, DisplayTrainees() Routine"
                QueryCallErrorCode1002()
            End Try
        End Sub
    Also, as before, since this is a new change request there are not yet any trainees assigned to the change request.
    The text of the buttons have been configured for trainees, instead of approvers. So the user can now click the Add Trainee(s) button.

    The same form is generated for the employees. However, because trainees can be added in groups or individually two radio buttons are now visible. and the combobox is not.

    See next post

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    The same form is generated for the employees. However, because trainees can be added in groups or individually two radio buttons are now visible. and the combobox is not.

    Name:  EmployeeTableONe.jpg
Views: 18
Size:  17.8 KB

    So, for this exercise we will add a single employee by selecting the By Individual radio button

    Code:
        Private Sub radIndividual_CheckedChanged(sender As Object, e As EventArgs) Handles radIndividual.CheckedChanged
            With lblInfo
                .Visible = True
                .TextAlign = ContentAlignment.MiddleLeft
                .Text = String.Format("Select a department using the Combobox.{0}Double Click in the row of any individual to be added to the Trainee List.",
                                      vbNewLine)
            End With
            cmbDepartment.Visible = True
            _intTrain = 2
        End Sub
    This opens the combobox and a selection is made from the combobox, as with the approvers previously.

    Name:  EmployeeListTwo.jpg
Views: 18
Size:  22.9 KB

    After that I then double click, as previously and select one of the displayed employees.

    Case = 2, so the following code is executed. Not really, it error's out.

    Code:
                Case = 2
                    'This method throws an exception and I have, so far, been unable to find why.  Further investigation will be required.
                    Me.tblChangeTrainBindingSource.AddNew()
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("intChangeID") = glbintCRNum
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strName") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strFullName")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strDept") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strDepartment")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strLogin") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strLogin")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strPassword") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strPassword")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strJobTitle") = CType(TblEmployeeBindingSource.Current, DataRowView).Item("strJobTitle")
                    CType(Me.tblChangeTrainBindingSource.Current, DataRowView).Item("strMasterBaseType") = glbstrBaseCategory
    Name:  ErrorMessage.jpg
Views: 18
Size:  47.3 KB

    Note that the code for this transfer of data is identical to that used for approvers, except that the receiving table tblChangeTrain, instead of tblChangeApprove.

    I have commented out (one at a time) every line of the transfer code above and on execution the error occurs at every line.
    So at this point I have no idea what or why the problem is. It is my guess that there is something in the code upstream of this that is causing the error, but I cannot spot what that is. In the mean time, I am using this code to transfer the data between the tables (for trainees) and it is working. However, it is not the code of my choice. and does not answer the question of why the other code throws an exception.

    [Code]
    'Get data from user table row
    'Send one row of data and add to training list
    Dim NewTrainingRow As _MasterBase4_0ItemMasterDataSet.tblChangeTrainRow
    NewTrainingRow = _MasterBase4_0ItemMasterDataSet.tblChangeTrain.NewtblChangeTrainRow()
    NewTrainingRow.intChangeID = glbintCRNum
    NewTrainingRow.StrName = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strFullName
    NewTrainingRow.strJobTitle = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strJobTitle
    NewTrainingRow.strDept = glbstrDept
    NewTrainingRow.strLogin = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strLogin
    NewTrainingRow.strPassword = _MasterBase4_0ItemMasterDataSet.tblEmployee(0).strPassword
    NewTrainingRow.strMasterBaseType = glbstrBaseCategory
    NewTrainingRow.blnRemoved = False
    _MasterBase4_0ItemMasterDataSet.tblChangeTrain.Rows.Add(NewTrainingRow)
    dgvEmployees.Enabled = False
    Me.Validate()
    Me.tblChangeTrainBindingSource.EndEdit()
    Me.TblChangeTrainTableAdapter.Update(Me._MasterBase4_0ItemMasterDataSet)
    Me.Close()
    [/Code}

  36. #36
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    3,798

    Re: System.InvalidCastException

    Oh, now I sort of get it.

    This line:
    Code:
    Me.tblChangeTrainBindingSource.AddNew()
    Obviously adds a thing to the BindingSource. The thing it adds is the "type" of data in the list. A BindingSource is basically a list. If you haven't gone pretty far out of your way to convince this BindingSource it is a list of a specific type, it's a list of type Object. So when you ask it to add a new thing, you get a new Object. Not a new DataRowView.

    In fact, it'd be weird for your binding source to contain DataRowViews. This is part of what makes DataTable so horrible and confusing. A DataTable is supposed to represent your program's data in tabular form. When you bind it to a DataGridView, the DGV converts the DataTables and DataRows into DataRowView objects based on column types and other properties you might have (but aren't required to have) configured. When it draws itself, it uses the data in the DataRowView objects to decide how to draw itself, in conjunction with about 4 other collection types so it knows what to do with each cell.

    So if you want to get DataRowViews, I don't think you should be working with the BindingSource. That's how you get to "the data that is converted to DataRow objects that make DataTables that are converted to DataGridViewRows in the DataGridView." You should be working with the DataGridView if you want to get DataGridViewRows. That's why your new code works: you ask a DataSet to make a row, and work with that DataRow object. That data set is probably the binding source for the table.

    I don't have specific advice because I'm running about 10 minutes late for an appointment, but I think that's the key. I'll look it over a little more later, personally I feel like everything surrounding the DataSet/DataTable approach to DGV is designed to be bewildering unless you're already a master at the MVC pattern.
    Nothing I post is production-ready. It is provided as-is, use it at your own risk.

  37. #37
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    After 2h and 45m of testing i managed to reproduce that exception!!!
    So i think your problem is do to your binding source does not have the datasource and datamember set...

  38. #38

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    OooooK. So exactly where is it that, that information should go. I ask this because I thought that I already had that in one of my routines that is run with this.

  39. #39
    Hyperactive Member Mike Storm's Avatar
    Join Date
    Jun 2017
    Location
    Belgium
    Posts
    425

    Re: System.InvalidCastException

    In the designer:

    https://imgur.com/a/Eo0xg

    Where it says DataSource set it to the dataset, datamember to the datatable

  40. #40

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2011
    Location
    Oregon City, Oregon
    Posts
    256

    Re: System.InvalidCastException

    Crap, I just looked on the form and the binding source for tblChangeTrain is empty as my whiskey glass right now. Damn, that is a piss-off. Let me put it in and see what happens.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.