-
Jan 12th, 2025, 12:55 AM
#1
Thread Starter
New Member
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"

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
-
Jan 12th, 2025, 02:28 AM
#2
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?
-
Jan 12th, 2025, 02:51 AM
#3
Thread Starter
New Member
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.
-
Jan 12th, 2025, 06:15 AM
#4
Re: HELP : The Given key was not present in the dictionary
This line
Code:
cmd.ExecuteNonQuery()
is line 285?
-
Jan 12th, 2025, 11:38 AM
#5
Thread Starter
New Member
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.
-
Jan 12th, 2025, 12:02 PM
#6
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?
-
Jan 12th, 2025, 12:04 PM
#7
Lively Member
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.
-
Jan 12th, 2025, 12:43 PM
#8
Thread Starter
New Member
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 ;
-
Jan 12th, 2025, 12:45 PM
#9
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by BlackRiver1987
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
-
Jan 12th, 2025, 12:53 PM
#10
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.
-
Jan 18th, 2025, 06:44 AM
#11
Thread Starter
New Member
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
-
Jan 18th, 2025, 06:45 AM
#12
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by OptionBase1
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
-
Jan 18th, 2025, 07:33 AM
#13
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by yogaadolf
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
-
Jan 18th, 2025, 07:36 AM
#14
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.
-
Jan 18th, 2025, 08:42 AM
#15
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by ChrisE
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.

I am very frustrated with this problem
-
Jan 18th, 2025, 09:26 AM
#16
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.
-
Jan 18th, 2025, 09:43 AM
#17
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by PlausiblyDamp
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()
-
Jan 18th, 2025, 10:50 AM
#18
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by yogaadolf
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.
-
Jan 18th, 2025, 07:51 PM
#19
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by PlausiblyDamp
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.
-
Jan 18th, 2025, 08:32 PM
#20
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.
-
Jan 19th, 2025, 07:05 AM
#21
Thread Starter
New Member
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by PlausiblyDamp
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
-
Jan 19th, 2025, 08:09 AM
#22
Re: HELP : The Given key was not present in the dictionary
 Originally Posted by yogaadolf
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|