我面临错误字典中没有给定的密钥。请告诉我如何解决它。 [英] I am facing error The given key was not present in the dictionary. Please tell me how can i resolve it.

查看:68
本文介绍了我面临错误字典中没有给定的密钥。请告诉我如何解决它。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub btnDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownload.Click
      
       If dgvAttachments.SelectedCells.Count = 1 AndAlso dgvAttachments.SelectedCells(0).Value IsNot Nothing Then
           DownloadFile(dgvAttachments.SelectedCells(0))

       Else
           MessageBox.Show("Select a single cell from Attachment column", "Error uploading file", MessageBoxButtons.OK, MessageBoxIcon.[Error])
      If
   End Sub
   Private Sub DownloadFile(ByVal dgvcell As DataGridViewCell)
       Dim _myAttachments As New Dictionary(Of Integer, Byte())
       Dim FileName As String = Convert.ToString(dgvcell.Value)
       If FileName = String.Empty Then
           Return
       End If
       Dim fileInfo As New FileInfo(FileName)
       Dim fileExtension As String = fileInfo.Extension
       Dim byteData As Byte() = Nothing
       Using saveFileDialog1 As New SaveFileDialog()
           saveFileDialog1.Filter = "Files (*" & fileExtension & ")|*" & fileExtension
           saveFileDialog1.Title = "Save File as"
           saveFileDialog1.CheckPathExists = True
           saveFileDialog1.FileName = FileName
           If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
               byteData = _myAttachments(dgvcell.RowIndex)
               File.WriteAllBytes(saveFileDialog1.FileName, byteData)
           End If
       End Using
      
   End Sub

推荐答案

您创建了一个新的词典,但是从不为它添加任何值。

然后你尝试访问具有不存在的键的该词典的值。



如果您在思考和分析方面付出最少的努力,这个问题很容易解决。 />


我的建议是学习使用调试器(Visual Studio中的F5),它可以让你实际获得大量的时间。
You create a new Dictionary, but you never add any value to it.
Then you try to access a value of that Dictionary with a key that is not present.

This problem is quite straightforward to solve, provided you put a minimum effort in thinking and analyze.

My advise would be to learn to use the debugger (F5 in Visual Studio), which can make you gain a significant amount of time actually.


这篇关于我面临错误字典中没有给定的密钥。请告诉我如何解决它。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆