Argument Exception - The path is not of a legal form (vb.net)

吃可爱长大的小学妹 提交于 2019-12-13 07:04:49

问题


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'

Save code

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)

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.

Chris


回答1:


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 " ".

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



来源:https://stackoverflow.com/questions/14739756/argument-exception-the-path-is-not-of-a-legal-form-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!