参数异常-路径不是合法形式(vb.net) [英] Argument Exception - The path is not of a legal form (vb.net)

查看:255
本文介绍了参数异常-路径不是合法形式(vb.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前在正在制作的程序中遇到的最令人讨厌的错误,对于能帮助我修复该问题的任何帮助或建议,我将深表感谢.我遇到问题的程序部分是一种将所选图像加载到图片框中,然后单击保存"按钮将其保存到MS Access数据库中的表单.执行"Browse_Click"事件时,它会提示您搜索图像位置并将其加载到图片框(pbImage)中.此位工作正常,并将其成功加载到图片框.我遇到的问题是,当我尝试将图像保存到访问数据库时,出现以下参数异常错误路径不是合法形式". 据我所知,我的所有代码都可以正常运行,因为它以前可以工作,但是在一两个小时前,这个错误突然开始出现.

I'm currently having the most irritating error in a program i'm making and i would seriously appreciate any help or advice that could help me fix it. The part of the program that i'm having a problem with is a form that loads up a selected image into a picturebox and then saves it into an MS Access database upon the click of the 'save' button. When executing the "Browse_Click" event, it prompts you to search for an image location and loads it into a picturebox (pbImage). This bit works fine and successfully loads it into it picturebox. The problem i'm having is when i try to save the image to my access database, i get the following argument exception error "The path is not of a legal form". As far as i know all my code is fully functional because it previously worked, however an hour or two ago this error suddenly started appearing.

下面的代码的第一部分是我要将图片加载到图片框中时执行的操作.下面的部分是保存"代码.

The first section of code below is what is executed when i want to load the picture into the picture box. The section below that is the 'save' code.

Public Class Manage_Cottages
Dim imgName As String
Dim daImage As OleDbDataAdapter
Dim dsImage As DataSet
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    Dim dlgImage As FileDialog = New OpenFileDialog()
    dlgImage.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
    If dlgImage.ShowDialog() = DialogResult.OK Then
        imgName = dlgImage.FileName
        Dim selectedFileName As String = dlgImage.FileName
        txtPath.Text = selectedFileName
        Dim newimg As New Bitmap(imgName)
        pbImage.SizeMode = PictureBoxSizeMode.StretchImage
        pbImage.Image = DirectCast(newimg, Image)
    End If
    dlgImage = Nothing
    imgName = " "
End Sub'

保存代码

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\Debug\CourseworkDatabase.mdb"
    Dim CN As New OleDbConnection(cnString)
    CN.Open()

     If imgName <> "" Then
        Dim fs As FileStream
        fs = New FileStream(imgName, FileMode.Open, FileAccess.Read) <----- where the error occurs.
        Dim picByte As Byte() = New Byte(fs.Length - 1) {}
        fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
        fs.Close()
        Dim strSQL As String
        strSQL = "INSERT INTO Cottage_Details([Image]) values (" & " @Img)"
        Dim imgParam As New OleDbParameter()
        imgParam.OleDbType = OleDbType.Binary
        imgParam.ParameterName = "Img"
        imgParam.Value = picByte
        Dim cmd As New OleDbCommand(strSQL, CN)
        cmd.Parameters.Add(imgParam)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Image successfully saved.")
        cmd.Dispose()
        CN.Close()
    End If
End Sub

下面也是即时窗口中显示的前几行(不确定是否有助于诊断问题)

Also below is the first couple of lines of what's displayed in the immediate window (not sure whether it will be of any help to diagnose the problem)

mscorlib.dll中发生了'System.ArgumentException'类型的第一次机会异常 System.Transactions严重:0: http://msdn. microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled 未处理的异常AlphaHolidayCottages.vshost.exeSystem.ArgumentException,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089法律形式.在System.IO.Path.NormalizePath(字符串路径,布尔型fullCheck,Int32 maxPathLength)

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAlphaHolidayCottages.vshost.exeSystem.ArgumentException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089The path is not of a legal form. at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)

感谢您的时间和帮助,如果有人可以帮助我解决问题,那就太过分了.

Thanks for your time and help, would be over the moon if someone could help me resolve the issue.

克里斯

推荐答案

btnBrowse_Click的末尾将imgName设置为",因此,当您将文件保存在btnSave_Click下时,您尝试将其保存到文件中.姓名 " ".

You set imgName to " " at the end of btnBrowse_Click, so when you save the file under btnSave_Click you are trying to save it to the file name " ".

尝试在btnBrowse_Click的末尾删除imgName = " ",或者在保存之前为imgName指定正确的文件名.

Try removing imgName = " " at the end of btnBrowse_Click, or assign imgName a proper file name before you save it.

这篇关于参数异常-路径不是合法形式(vb.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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