12 Replies - 1732 Views - Last Post: 07 June 2012 - 08:53 AM Rate Topic: -----

#1 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

'specified cast is not valid' error

Posted 06 June 2012 - 02:22 AM

Hello,
I'm a beginner of programming. I'm trying to make simple payroll system using vb.net 2003 and Access 2007. When I press 'delete' button in this application, having a "Specified cast is not valid" error.
conPayroll.Open()
                    Dim strSQL As String
                    strSQL = "select*from EMT where Empno='" & mskEmpno.ClipText & "'"
                    comEMT.CommandText = strSQL
                    drEMT = comEMT.ExecuteReader
                    If strStatus = "Add" Then
                        If drEMT.Read Then
                            MessageBox.Show("Record already exist-Enter another Employee no.", "Record existance check", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            mskEmpno.defaultText = "_____"
                            mskEmpno.Focus()
                        Else
                            txtNa.Enabled = True
                            gbPersonal.Enabled = True
                            gbEmployeement.Enabled = True
                            txtNa.Focus()
                        End If
                    Else
                        If Not drEMT.Read Then
                            MessageBox.Show("Record doest not exist-Enter another employee no", "Record existance check", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            mskEmpno.defaultText = "_____"
                            mskEmpno.Focus()
                        Else
                            txtNa.Enabled = True
                            gbPersonal.Enabled = True
                            gbEmployeement.Enabled = True

                            [b]Call display_Record()
                            If strStatus = "Edit" Then
                                mskEmpno.Focus()
                            Else
                                Dim Ans As DialogResult
                                Ans = MessageBox.Show("Do you want to delete this record?", "Conformation for deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                                drEMT.Close()

                                Dim strSQL2 As String
                                strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
                                comEMT.ExecuteNonQuery()
                                MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)[/b]



How can I solve this problem?
Thank you,


Is This A Good Question/Topic? 0
  • +

Replies To: 'specified cast is not valid' error

#2 John-Ellis  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 305
  • Joined: 23-March 10

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 05:37 AM

View Postnushi9, on 06 June 2012 - 02:22 AM, said:

Hello,
I'm a beginner of programming. I'm trying to make simple payroll system using vb.net 2003 and Access 2007. When I press 'delete' button in this application, having a "Specified cast is not valid" error.
conPayroll.Open()
                    Dim strSQL As String
                    strSQL = "select*from EMT where Empno='" & mskEmpno.ClipText & "'"
                    comEMT.CommandText = strSQL
                    drEMT = comEMT.ExecuteReader
                    If strStatus = "Add" Then
                        If drEMT.Read Then
                            MessageBox.Show("Record already exist-Enter another Employee no.", "Record existance check", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            mskEmpno.defaultText = "_____"
                            mskEmpno.Focus()
                        Else
                            txtNa.Enabled = True
                            gbPersonal.Enabled = True
                            gbEmployeement.Enabled = True
                            txtNa.Focus()
                        End If
                    Else
                        If Not drEMT.Read Then
                            MessageBox.Show("Record doest not exist-Enter another employee no", "Record existance check", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            mskEmpno.defaultText = "_____"
                            mskEmpno.Focus()
                        Else
                            txtNa.Enabled = True
                            gbPersonal.Enabled = True
                            gbEmployeement.Enabled = True

                            [b]Call display_Record()
                            If strStatus = "Edit" Then
                                mskEmpno.Focus()
                            Else
                                Dim Ans As DialogResult
                                Ans = MessageBox.Show("Do you want to delete this record?", "Conformation for deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                                drEMT.Close()

                                Dim strSQL2 As String
                                strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
                                comEMT.ExecuteNonQuery()
                                MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)[/b]



How can I solve this problem?
Thank you,


Hi,

I have never used MS Access to hold my information but, make sure you have primary key's set in your databases, this will allow you to delete and save new records in your databases.

Thanks

John
Was This Post Helpful? 1
  • +
  • -

#3 fatbunny  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 09-February 09

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 06:40 AM

Probably not related to the cast error but you are missing the line "comEMT.commandtext = strSQL2" in the last section

I added it in here


 Dim strSQL2 As String
                                strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
                                comEMT.commandtext = strSQL2
                                comEMT.ExecuteNonQuery()
                                MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)[/b]


This post has been edited by fatbunny: 06 June 2012 - 06:46 AM

Was This Post Helpful? 1
  • +
  • -

#4 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 07:21 AM

Mr.Jhon,
Yeah I did it but it's not working :( anyway Thank you very much for your help,it's really useful :)
reputation + :)

Was This Post Helpful? 0
  • +
  • -

#5 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 07:26 AM

View Postfatbunny, on 06 June 2012 - 06:40 AM, said:

Probably not related to the cast error but you are missing the line "comEMT.commandtext = strSQL2" in the last section

I added it in here


 Dim strSQL2 As String
                                strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
                                comEMT.commandtext = strSQL2
                                comEMT.ExecuteNonQuery()
                                MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)[/b]



Thank you very much Mr.fatbunny :) I added that line..but still have that error..
Was This Post Helpful? 0
  • +
  • -

#6 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 07:40 AM

Hey try this:

          conPayroll.open()
        Dim cm As New OleDb.OleDbCommand(" DELETE from EMT WHERE Empno = " & mskEmpno.ClipText, conPayroll)
        Try
            cm.ExecuteScalar()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conPayroll.Close()
        End Try
    End Sub

This post has been edited by m_wylie85: 06 June 2012 - 07:41 AM

Was This Post Helpful? 1
  • +
  • -

#7 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 09:18 AM

View Postm_wylie85, on 06 June 2012 - 07:40 AM, said:

Hey try this:

          conPayroll.open()
        Dim cm As New OleDb.OleDbCommand(" DELETE from EMT WHERE Empno = " & mskEmpno.ClipText, conPayroll)
        Try
            cm.ExecuteScalar()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            conPayroll.Close()
        End Try
    End Sub


Thank you.. I'll try.
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: 'specified cast is not valid' error

Posted 06 June 2012 - 07:43 PM

what line of code is causing the error? This usually happens when you're trying to plug a null value into a place that doesn't accept null values, for example trying to set a text box's Text property to a data field value that is null.
Was This Post Helpful? 1
  • +
  • -

#9 fatbunny  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 33
  • Joined: 09-February 09

Re: 'specified cast is not valid' error

Posted 07 June 2012 - 03:50 AM

you could try this.

Dim strSQL2 As String
     strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
     Dim comDA as new oledb.oledbdataAdapter
     comDAdeletecommand = new oledb.oledbdataAdapter(strSQL2, conpayroll)
     comDA.deletecommand.ExecuteNonQuery()
     MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)

Was This Post Helpful? 1
  • +
  • -

#10 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: 'specified cast is not valid' error

Posted 07 June 2012 - 07:05 AM

fatbunny, I don't think that will help. IMO It's not a matter of not being able to see the dataAdapter object, it's a matter of something wrong with the data. Probably because there's something wrong with the ClipText.
Was This Post Helpful? 1
  • +
  • -

#11 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,507
  • Joined: 16-March 11

Re: 'specified cast is not valid' error

Posted 07 June 2012 - 07:49 AM

You need to place a linebreak at the ClipText and see if it contains a value. I don't think anyone should be sitting here trying to guess what the problem is with your code. You need to learn to do that work yourself. If that's not where the error is then continue to place linebreaks until you find the source of your errors.
Was This Post Helpful? 1
  • +
  • -

#12 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

Re: 'specified cast is not valid' error

Posted 07 June 2012 - 08:05 AM

View PostBobRodes, on 06 June 2012 - 07:43 PM, said:

what line of code is causing the error? This usually happens when you're trying to plug a null value into a place that doesn't accept null values, for example trying to set a text box's Text property to a data field value that is null.


Thank you very much Mr.BobRodes.. I'll try to do that.

View Postfatbunny, on 07 June 2012 - 03:50 AM, said:

you could try this.

Dim strSQL2 As String
     strSQL2 = "delete from EMT where Empno='" & mskEmpno.ClipText & "'"
     Dim comDA as new oledb.oledbdataAdapter
     comDAdeletecommand = new oledb.oledbdataAdapter(strSQL2, conpayroll)
     comDA.deletecommand.ExecuteNonQuery()
     MessageBox.Show("Record is delete from EMT sucvessfully", "Deleteing and existing record", MessageBoxButtons.OK, MessageBoxIcon.Information)


It's not working..anyway, thank you very much for your help Mr.fatbunny :)
Was This Post Helpful? 0
  • +
  • -

#13 nushi9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-June 12

Re: 'specified cast is not valid' error

Posted 07 June 2012 - 08:53 AM

View Posttrevster344, on 07 June 2012 - 07:49 AM, said:

You need to place a linebreak at the ClipText and see if it contains a value. I don't think anyone should be sitting here trying to guess what the problem is with your code. You need to learn to do that work yourself. If that's not where the error is then continue to place linebreaks until you find the source of your errors.


Thank you very much for your advise..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1