HI ,
I'm currently creating a system for medical record for our school project
i was wondering , if someone could help me.. i always get this error " Couldn't find 'C:/User/...' " i coudnt retrieve the filename of the picture so that i can store it in my database , BUT when I try my back up file it works .it save image in the Database
It's the same code , i did not change anything

here is a part my code.. please help :(

Private Sub MDISaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click

        If Not didPreviouslyConnect Then
            MsgBox("Connecting to database")
        End If
        Dim isConnecting As Boolean = True
        While isConnecting

            oFullname = RegistrationForm.FirstNameTxt.Text & " " & RegistrationForm.MiddleInitialTxt.Text & " " & RegistrationForm.LastNameTxt.Text
            oBirthday = RegistrationForm.MonthCBtxt.Text & RegistrationForm.DayCBtxt.Text & RegistrationForm.YearCBtxt.Text


            Dim arrImage() As Byte
            arrImage = System.IO.File.ReadAllBytes(RegistrationForm.OpenFileDialog1.FileName)

            Try

                Dim cmd As New SqlCommand("Insert into Patient_TB(Patient_ID, PatientName,PatientAddress,ContactNumber,Height,Weight,Gender,Bloodtype,Birthday,PatientPicture) " & _
             " values( '" & RegistrationForm.PatientNoText.Text & "', '" & oFullname & "','" & RegistrationForm.AddressText.Text & "', '" & Trim(RegistrationForm.ContactNoTxt.Text) & "', '" & RegistrationForm.HeightTxtBox.Text & "', '" & RegistrationForm.WeightTxtbox.Text & "', '" + RegistrationForm.GenderCBtxt.SelectedItem + "', '" & RegistrationForm.BTypeTxt.Text & "','" & oBirthday & "',@PatientPicture)", dataconnect)

                cmd.Parameters.Add(New SqlParameter("@PatientPicture", _
                            SqlDbType.Image, arrImage.Length)).Value = arrImage
                dataconnect.Open()
                cmd.ExecuteNonQuery()
                dataconnect.Close()


                isConnecting = False
                didPreviouslyConnect = True

                MessageBox.Show("Saved to the database", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Information)

            Catch



            End Try



        End While

thanks best of luck :)

Dani AI

Generated

A few likely causes and a robust pattern to avoid them.

Common causes

  • The file path being read is empty because the OpenFileDialog was never shown on the same instance that the save code reads from (default-instance vs created instance).
  • The selected file was moved/deleted or the code runs before a selection is stored.
  • The file is locked by the UI (for example Image.FromFile can keep a lock) or the code swallows the exception (an empty Catch hides the real error).

What helped in this thread

  • pointed toward showing the dialog and using the returned filename; that is the right approach. later confirmed the problem was resolved. For others, follow a pattern that validates the selection, keeps the path in a stable place (a form property or a TextBox/Tag), and reports the exact exception if something fails.

Safe pattern (VB.NET)

  • Keep the selected path in a public property on the registration form when the user picks a file.
  • Before saving, check that the path is not empty and that the file exists.
  • Read the bytes via a FileStream (or File.ReadAllBytes) inside a Using block.
  • Use Using blocks for SqlConnection/SqlCommand, parameterize every value, and prefer varbinary(max) instead of the deprecated image type.
  • Do not leave Catch blocks empty—log or show ex.Message.

Example (illustrative)

' RegistrationForm.SelectedImagePath is set when the user chooses the file
If String.IsNullOrEmpty(RegistrationForm.SelectedImagePath) OrElse Not IO.File.Exists(RegistrationForm.SelectedImagePath) Then
    MessageBox.Show("Image not selected or not found.")
    Return
End If

Dim bytes() As Byte
Using fs As New IO.FileStream(RegistrationForm.SelectedImagePath, IO.FileMode.Open, IO.FileAccess.Read)
    ReDim bytes(CInt(fs.Length) - 1)
    fs.Read(bytes, 0, bytes.Length)
End Using

Using cn As New SqlConnection(connectionString)
    Using cmd As New SqlCommand("INSERT INTO Patient_TB (..., PatientPicture) VALUES (..., @Image)", cn)
        cmd.Parameters.AddWithValue("@PatientID", patientId)
        cmd.Parameters.Add("@Image", SqlDbType.VarBinary, bytes.Length).Value = bytes
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Using

Extra tips

  • Avoid relying on another form's controls directly; expose a property instead.
  • If the UI displays the image, load it from a MemoryStream to avoid locking the original file.
  • Consider storing file paths (or using a file store) instead of large binaries in the DB for scalability.

Recommended Answers

All 3 Replies

Dim arrImage() As Byte
        Dim openFile As New OpenFileDialog
        If openFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
            arrImage = System.IO.File.ReadAllBytes(openFile.FileName)
        Else
            Exit Sub
        End If

Thanks sir, i already figure it out .. thanks for your answer..

Then please mark this thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.