Results 1 to 22 of 22

Thread: HELP : The Given key was not present in the dictionary

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Lightbulb HELP : The Given key was not present in the dictionary

    Hi, I'm here, asking for help with the problem below.

    I tried to run the save procedure in vb.net with the mysql hosting database, when it was run the following error message appeared. "The Given key was not present in the dictionary"
    Name:  ERROR.jpg
Views: 349
Size:  45.2 KB
    Code:
     Private Sub btnsimpan_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click
     buka()
                    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
                    cmd = New MySqlCommand()
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.CommandText = "tbl_karyawan_tambah"
                    cmd.Parameters.Add("tid_karyawan", MySqlDbType.Int32, 5).Value = tid_karyawan.Text.Trim
                    cmd.Parameters.Add("tnama_karyawan", MySqlDbType.VarChar, 50).Value = tnama_karyawan.Text.Trim
                    cmd.Parameters.Add("talamat", MySqlDbType.VarChar, 70).Value = talamat.Text.Trim
                    cmd.Parameters.Add("tjk", MySqlDbType.VarChar, 15).Value = tgender
                    cmd.Parameters.Add("tkota", MySqlDbType.VarChar, 35).Value = tkota.Text.Trim
                    cmd.Parameters.Add("ttmk", MySqlDbType.Date).Value = ttmk.Value
                    cmd.Parameters.Add("tktp", MySqlDbType.VarChar, 20).Value = tktp.Text.Trim
                    cmd.Parameters.Add("ttlp", MySqlDbType.VarChar, 20).Value = ttlp.Text.Trim
                    cmd.Parameters.Add("tstatus_karyawan", MySqlDbType.VarChar, 10).Value = tstatus_karyawan
                    cmd.Parameters.Add("ttgl_kontrak", MySqlDbType.Date).Value = If(RadioButton4.Checked = True, DBNull.Value, tkontrak.Value)
                    cmd.Parameters.Add("tid_sts_pajak", MySqlDbType.Int32, 2).Value = tpajak
                    cmd.Parameters.Add("tnpwp", MySqlDbType.VarChar, 20).Value = tnpwp.Text.Trim
                    cmd.Parameters.Add("tid_bank", MySqlDbType.Int32, 2).Value = tbank
                    cmd.Parameters.Add("trek", MySqlDbType.VarChar, 12).Value = trek.Text.Trim
                    cmd.Parameters.Add("tid_departemen", MySqlDbType.Int32, 2).Value = tdept
                    cmd.Parameters.Add("tid_jabatan", MySqlDbType.Int32, 2).Value = tjabatan
                    cmd.Parameters.Add("tcreatedby", MySqlDbType.VarChar, 15).Value = UTAMA.tuserid.Text.Trim
                    cmd.Parameters.Add("tcreateddt", MySqlDbType.Date).Value = Format(Now(), "yyyy-MM-dd")
                    cmd.Parameters.Add("tstatus", MySqlDbType.VarChar, 10).Value = tstatus
                    cmd.Connection = koneksi
                    cmd.ExecuteNonQuery()
                    Me.Cursor = System.Windows.Forms.Cursors.Default
                    tampilData()
                    MsgBox("Data berhasil disimpan.")
                    btntambah.Enabled = True
                    btnsimpan.Enabled = False
                    btnbatal.Enabled = False
                    btnhapus.Enabled = False
                    btnrubah.Enabled = False
                    kosong()
                    mati()
    End If
            Catch ex As Exception
                MsgBox(ex.Message & vbNewLine & _
                ex.ToString & vbNewLine & "Data gagal disimpan." & vbNewLine &
                "Mohon segera dicek kembali datanya.")
                Me.Cursor = System.Windows.Forms.Cursors.Default
                Exit Sub
            End Try
        End Sub
    Last edited by Shaggy Hiker; Jan 12th, 2025 at 10:10 AM. Reason: Added CODE

  2. #2
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    491

    Re: HELP : The Given key was not present in the dictionary

    Have you made sure that each parameter you are attempting to send a value for exists in the stored procedure you're executing?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Yes. I have confirmed every parameter in the procedure. Because it was possible before.

  4. #4
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    491

    Re: HELP : The Given key was not present in the dictionary

    This line
    Code:
    cmd.ExecuteNonQuery()
    is line 285?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Yes, it is true.
    cmd.ExecuteNonQuery()

    But when I run with the localhost database the transaction process has no problems.

  6. #6
    Hyperactive Member
    Join Date
    Jul 2022
    Location
    Buford, Ga USA
    Posts
    491

    Re: HELP : The Given key was not present in the dictionary

    I see, maybe there is a rights issue with the user you're using to connect to the remote server with?

  7. #7
    Lively Member
    Join Date
    Jun 2017
    Posts
    83

    Re: HELP : The Given key was not present in the dictionary

    If I had to guess, your local and production stored procedures are not the same. Check both and compare input parameters.

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    DELIMITER //
    CREATE PROCEDURE `tbl_karyawan_tambah`(IN `tid_karyawan` INT, IN `tnama_karyawan` VARCHAR(50), IN `talamat` VARCHAR(70), IN `tkota` VARCHAR(35), IN `tjk` VARCHAR(15), IN `tktp` VARCHAR(20), IN `ttlp` VARCHAR(20), IN `tid_sts_pajak` INT, IN `tnpwp` VARCHAR(20), IN `tid_departemen` INT, IN `tid_jabatan` INT, IN `tstatus_karyawan` VARCHAR(10), IN `ttmk` DATE, IN `tid_bank` INT, IN `trek` VARCHAR(12), IN `tcreatedby` VARCHAR(15), IN `tcreateddt` DATE, IN `tstatus` VARCHAR(10), IN `ttgl_kontrak` DATE)
    BEGIN
    INSERT INTO tbl_karyawan(id_karyawan,nama_karyawan,alamat,kota,jk,ktp,tlp,id_sts_pajak,npwp,id_departemen,id_jab atan,status_karyawan,tmk,id_bank,rek,createdby,createddt,status)
    VALUES(tid_karyawan,tnama_karyawan,talamat,tkota,tjk,tktp,ttlp,tid_sts_pajak,tnpwp,tid_departemen,ti d_jabatan,tstatus_karyawan,ttmk,tid_bank,trek,tcreatedby,tcreateddt,tstatus);
    END //
    DELIMITER ;

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by BlackRiver1987 View Post
    If I had to guess, your local and production stored procedures are not the same. Check both and compare input parameters.
    No.

    Previously it was no problem even on hosting.
    Having problems since a few weeks ago

  10. #10
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,467

    Re: HELP : The Given key was not present in the dictionary

    Your earlier VB code has parameters in a different order than the SQL code above. No idea if that is relevant or not with MySQL like it was (and possibly still is) with Access.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    I have changed and adjusted the order between the query in VB and the procedure but the error is still the same

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by OptionBase1 View Post
    Your earlier VB code has parameters in a different order than the SQL code above. No idea if that is relevant or not with MySQL like it was (and possibly still is) with Access.
    I have changed and adjusted the order between the query in VB and the procedure but the error is still the same

  13. #13
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,435

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by yogaadolf View Post
    No.

    Previously it was no problem even on hosting.
    Having problems since a few weeks ago
    Then it’s not code related, but an user access rights issue

  14. #14
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,110

    Re: HELP : The Given key was not present in the dictionary

    where does the Connecton Open take place ?
    Code:
    '.....
       With cmd
                .CommandType = CommandType.StoredProcedure
                .Connection.Open()  '<---- 
    '.....
    I dont see anything in Post#1
    that indicates you have a Open Connection
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by ChrisE View Post
    where does the Connecton Open take place ?
    Code:
    '.....
       With cmd
                .CommandType = CommandType.StoredProcedure
                .Connection.Open()  '<---- 
    '.....
    I dont see anything in Post#1
    that indicates you have a Open Connection
    This is to open a connection
    "buka()"


    If I add what you mentioned earlier, a different error will appear again.
    Name:  error 2.jpg
Views: 234
Size:  29.6 KB

    I am very frustrated with this problem

  16. #16
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: HELP : The Given key was not present in the dictionary

    The error seems to indicate that you haven't set a connection for the command.

  17. #17

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by PlausiblyDamp View Post
    The error seems to indicate that you haven't set a connection for the command.
    I have set up the connection for command. is in buka()

  18. #18
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by yogaadolf View Post
    I have set up the connection for command. is in buka()
    Could you post the code you are using to set the connection for the command then? Please post the code as text, not a screenshot btw.

    Also, if you put a breakpoint on that line and run your code in the debugger, inspect the variables on that line and see which are null / nothing - the error indicates something on that line isn't set.

  19. #19

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by PlausiblyDamp View Post
    Could you post the code you are using to set the connection for the command then? Please post the code as text, not a screenshot btw.

    Also, if you put a breakpoint on that line and run your code in the debugger, inspect the variables on that line and see which are null / nothing - the error indicates something on that line isn't set.
    Code:
    Public Function app_path() As String
            Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function
    
    Public Sub buka()
            Dim objBacaFile As New StreamReader(app_path() + "\CONFIG\Config.txt")
            line1 = objBacaFile.ReadLine
            line2 = objBacaFile.ReadLine
            line3 = objBacaFile.ReadLine
            tdns = Mid(objBacaFile.ReadLine, 5)
            tserver = Mid(objBacaFile.ReadLine, 8)
            tdatabase = Mid(objBacaFile.ReadLine, 10)
            tuser = Mid(objBacaFile.ReadLine, 6)
            tpwd = Mid(objBacaFile.ReadLine, 5)
            tbackup = Mid(objBacaFile.ReadLine, 11)
            tDownloadPath = Mid(objBacaFile.ReadLine, 14)
            tFtpURL = Mid(objBacaFile.ReadLine, 8)
            tFtpUser = Mid(objBacaFile.ReadLine, 9)
            tFtpPwd = Mid(objBacaFile.ReadLine, 8)
            tport = Mid(objBacaFile.ReadLine, 6)
            poling = Mid(objBacaFile.ReadLine, 8)
            chartS = Mid(objBacaFile.ReadLine, 8)
            objBacaFile.Close()
    
            str = "Server='" & tserver & "';user id='" & tuser & "';password='" & Decrypt(tpwd, 10) & "';database='" & tdatabase & "'"
            koneksi = New MySqlConnection(str)
            Try
            If koneksi.State = ConnectionState.Closed Then
                koneksi.Open()
            End If
                Catch ex As Exception
                MsgBox(Err.Description, MsgBoxStyle.Critical, "Error")
                End
            End Try
    End Sub
    Last edited by dday9; Jan 18th, 2025 at 10:37 PM.

  20. #20
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: HELP : The Given key was not present in the dictionary

    I can see you are opening a connection in that method, however I don't see anywhere you get the command object to use it.

  21. #21

    Thread Starter
    New Member
    Join Date
    Jan 2025
    Posts
    13

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by PlausiblyDamp View Post
    I can see you are opening a connection in that method, however I don't see anywhere you get the command object to use it.
    Private Sub btnsimpan_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click
    buka()
    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
    cmd = New MySqlCommand()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "tbl_karyawan_tambah"
    cmd.Parameters.Add("tid_karyawan", MySqlDbType.Int32, 5).Value = tid_karyawan.Text.Trim
    cmd.Parameters.Add("tnama_karyawan", MySqlDbType.VarChar, 50).Value = tnama_karyawan.Text.Trim
    cmd.Parameters.Add("talamat", MySqlDbType.VarChar, 70).Value = talamat.Text.Trim
    cmd.Parameters.Add("tjk", MySqlDbType.VarChar, 15).Value = tgender
    cmd.Parameters.Add("tkota", MySqlDbType.VarChar, 35).Value = tkota.Text.Trim
    cmd.Parameters.Add("ttmk", MySqlDbType.Date).Value = ttmk.Value
    cmd.Parameters.Add("tktp", MySqlDbType.VarChar, 20).Value = tktp.Text.Trim
    cmd.Parameters.Add("ttlp", MySqlDbType.VarChar, 20).Value = ttlp.Text.Trim
    cmd.Parameters.Add("tstatus_karyawan", MySqlDbType.VarChar, 10).Value = tstatus_karyawan
    cmd.Parameters.Add("ttgl_kontrak", MySqlDbType.Date).Value = If(RadioButton4.Checked = True, DBNull.Value, tkontrak.Value)
    cmd.Parameters.Add("tid_sts_pajak", MySqlDbType.Int32, 2).Value = tpajak
    cmd.Parameters.Add("tnpwp", MySqlDbType.VarChar, 20).Value = tnpwp.Text.Trim
    cmd.Parameters.Add("tid_bank", MySqlDbType.Int32, 2).Value = tbank
    cmd.Parameters.Add("trek", MySqlDbType.VarChar, 12).Value = trek.Text.Trim
    cmd.Parameters.Add("tid_departemen", MySqlDbType.Int32, 2).Value = tdept
    cmd.Parameters.Add("tid_jabatan", MySqlDbType.Int32, 2).Value = tjabatan
    cmd.Parameters.Add("tcreatedby", MySqlDbType.VarChar, 15).Value = UTAMA.tuserid.Text.Trim
    cmd.Parameters.Add("tcreateddt", MySqlDbType.Date).Value = Format(Now(), "yyyy-MM-dd")
    cmd.Parameters.Add("tstatus", MySqlDbType.VarChar, 10).Value = tstatus
    cmd.Connection = koneksi
    cmd.ExecuteNonQuery()
    Me.Cursor = System.Windows.Forms.Cursors.Default
    tampilData()
    MsgBox("Data berhasil disimpan.")
    btntambah.Enabled = True
    btnsimpan.Enabled = False
    btnbatal.Enabled = False
    btnhapus.Enabled = False
    btnrubah.Enabled = False
    kosong()
    mati()
    End If
    Catch ex As Exception
    MsgBox(ex.Message & vbNewLine & _
    ex.ToString & vbNewLine & "Data gagal disimpan." & vbNewLine &
    "Mohon segera dicek kembali datanya.")
    Me.Cursor = System.Windows.Forms.Cursors.Default
    Exit Sub
    End Try
    End Sub

  22. #22
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,836

    Re: HELP : The Given key was not present in the dictionary

    Quote Originally Posted by yogaadolf View Post
    Private Sub btnsimpan_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click
    buka()
    Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
    cmd = New MySqlCommand()
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "tbl_karyawan_tambah"
    cmd.Parameters.Add("tid_karyawan", MySqlDbType.Int32, 5).Value = tid_karyawan.Text.Trim
    cmd.Parameters.Add("tnama_karyawan", MySqlDbType.VarChar, 50).Value = tnama_karyawan.Text.Trim
    cmd.Parameters.Add("talamat", MySqlDbType.VarChar, 70).Value = talamat.Text.Trim
    cmd.Parameters.Add("tjk", MySqlDbType.VarChar, 15).Value = tgender
    cmd.Parameters.Add("tkota", MySqlDbType.VarChar, 35).Value = tkota.Text.Trim
    cmd.Parameters.Add("ttmk", MySqlDbType.Date).Value = ttmk.Value
    cmd.Parameters.Add("tktp", MySqlDbType.VarChar, 20).Value = tktp.Text.Trim
    cmd.Parameters.Add("ttlp", MySqlDbType.VarChar, 20).Value = ttlp.Text.Trim
    cmd.Parameters.Add("tstatus_karyawan", MySqlDbType.VarChar, 10).Value = tstatus_karyawan
    cmd.Parameters.Add("ttgl_kontrak", MySqlDbType.Date).Value = If(RadioButton4.Checked = True, DBNull.Value, tkontrak.Value)
    cmd.Parameters.Add("tid_sts_pajak", MySqlDbType.Int32, 2).Value = tpajak
    cmd.Parameters.Add("tnpwp", MySqlDbType.VarChar, 20).Value = tnpwp.Text.Trim
    cmd.Parameters.Add("tid_bank", MySqlDbType.Int32, 2).Value = tbank
    cmd.Parameters.Add("trek", MySqlDbType.VarChar, 12).Value = trek.Text.Trim
    cmd.Parameters.Add("tid_departemen", MySqlDbType.Int32, 2).Value = tdept
    cmd.Parameters.Add("tid_jabatan", MySqlDbType.Int32, 2).Value = tjabatan
    cmd.Parameters.Add("tcreatedby", MySqlDbType.VarChar, 15).Value = UTAMA.tuserid.Text.Trim
    cmd.Parameters.Add("tcreateddt", MySqlDbType.Date).Value = Format(Now(), "yyyy-MM-dd")
    cmd.Parameters.Add("tstatus", MySqlDbType.VarChar, 10).Value = tstatus
    cmd.Connection = koneksi
    cmd.ExecuteNonQuery()
    Me.Cursor = System.Windows.Forms.Cursors.Default
    tampilData()
    MsgBox("Data berhasil disimpan.")
    btntambah.Enabled = True
    btnsimpan.Enabled = False
    btnbatal.Enabled = False
    btnhapus.Enabled = False
    btnrubah.Enabled = False
    kosong()
    mati()
    End If
    Catch ex As Exception
    MsgBox(ex.Message & vbNewLine & _
    ex.ToString & vbNewLine & "Data gagal disimpan." & vbNewLine &
    "Mohon segera dicek kembali datanya.")
    Me.Cursor = System.Windows.Forms.Cursors.Default
    Exit Sub
    End Try
    End Sub
    Firstly it would help if you could explain a bit more about the problem and not just dump unformat, unexplained code here.

    Secondly, that code is different to the code in the screenshot above, so I have no idea if the new code is working or not - and if not how it is failing. The more information you can give makes it easier for us to help.

    Now... in your original post you were trying to open the connection near the top of the code block, but you were setting the connection right near the bottom after you try to open it. You need to set the connection before opening it. This would explain the error about the connection being null, it was indeed null when you called .Open on it. If you had put a breakpoint on the line and tried to debug this it would have show this to be the case in the debugger.

    I have no idea exactly what error you are getting in the second block of code, however you don't seem to be opening the connection anywhere - I am guessing that is the problem.

Posting Permissions

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



Click Here to Expand Forum to Full Width