0

I'm newbie here and I need help to fix my code problem. My project is about storing PDF files to MySQL database using vb.net code. I have problem with these code bellow that cure that cause:

Error:

The given path's format is not supported

and here is my codes and my tables on database use MediumBLOB as the data type

Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click

    'codes to save the pdf
    Dim filesize As UInt32
    Dim rawData() As Byte = IO.File.ReadAllBytes(strFilename)
    Dim fs As FileStream

    Try
        fs = New FileStream("'" & strFilename & "'", FileMode.Open, FileAccess.Read)
        filesize = fs.Length

        rawData = New Byte(filesize) {}
        fs.Read(rawData, 0, filesize)
        fs.Close()

        conn.Open()
        cmd = New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO simpanambilpdf VALUES (NULL, @FileName, @FileSize, @File)", conn)
        cmd.Parameters.AddWithValue("@FileName", strFileName)
        cmd.Parameters.AddWithValue("@FileSize", filesize)
        cmd.Parameters.AddWithValue("@File", rawData)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Congratulations PDF file is saved!", "HORE", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub 

Please offer me some answer or adviced. Thank you

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Why are you putting the file name inside single-quotes when creating the `FileSteam` and saving to the database? Notice how you don't do that when calling `ReadAllBytes` and (I'm assuming) it works there? When have you ever seen a file name/path wrapped in single-quotes? – jmcilhinney Jul 12 '18 at 05:02
  • Why are you creating that `FileStream` at all? You've got the file contents in a `Byte` array so why do you need a `FileStream` to get the length? Even if you couldn't get that value from the `Byte` array, the `FileInfo` class has a `Length` property that would give it to you without opening the file a second time. – jmcilhinney Jul 12 '18 at 05:06
  • Argh! I just realised that you're actually even reading the data from the `FileStream`. Why are you reading the file twice? The `File.ReadAllBytes` method reads all the bytes of the file into an array. That's all you need. No `FileStream`. – jmcilhinney Jul 12 '18 at 05:07
  • I'm just copying that code from https://dev.mysql.com/doc/connector-net/en/connector-net-programming-blob-writing.html and modify it. And I've try without the Filestream and it cause another error: "Object reference not set to an instance of an object. – eleonora anggi Jul 12 '18 at 06:17
  • But that example doesn't call `File.ReadAllBytes` first. The point of the `FileStream` in the example is to read the data from the file but, if you're doing that with `ReadAllBytes`, you don't need the `FileStream`. Maybe that example was written before .NET 2.0, when that `ReadAllBytes` method was added, or perhaps the author just didn't know it existed. Either way, you don't need a `FileStream`. If you got a `NullReferenceException` it's because you didn't remove ALL references to it. Get rid of the `fs` declaration and then any other line that is in error as a result. – jmcilhinney Jul 12 '18 at 06:29
  • As I already said, you get the `filesize` value from the `Length` of the `Byte` array. – jmcilhinney Jul 12 '18 at 06:30
  • Okay by then, after I remove all the `Filestream` and only use `File.ReadAllBytes` and inside the `Try` I still use `rawdata = New Byte(OpenFileDialog1.Filename){}` so the "Object reference not set to an instance of an object" error not appear and another error show up, that is "Conversion from string "C:\User\Documents" to integer was not valid" – eleonora anggi Jul 12 '18 at 07:56
  • That line you've added makes absolutely no sense at all. For one thing, where have you seen any indication that you can create an array like that? Most importantly though, why on Earth are you trying to create an array at all!? I've already told you that `File.ReadAllBytes` reads the contents of the file and returns it as a `Byte` array. If `rawdata` already refers to a `Byte` array containing the file contents, why are creating a new `Byte` array and assigning it to `rawdata`? STOP IT! – jmcilhinney Jul 12 '18 at 14:41
  • I know that line was no sense, but when I remove it the error will be "Object reference not set to an instance of an object" again and I don't have any idea about that error. I've no clue about that error, fyi the codes I posted above was the n modification cause the "Object reference ..." error always appear previously – eleonora anggi Jul 13 '18 at 01:31

1 Answers1

0

I can't bear this any longer so I'm just going to provide the code:

Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsimpan.Click
    'codes to save the pdf
    Dim rawData() As Byte = IO.File.ReadAllBytes(strFilename)
    Dim filesize As UInt32 = rawData.Length

    Try    
        conn.Open()
        cmd = New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO simpanambilpdf VALUES (NULL, @FileName, @FileSize, @File)", conn)
        cmd.Parameters.AddWithValue("@FileName", strFileName)
        cmd.Parameters.AddWithValue("@FileSize", filesize)
        cmd.Parameters.AddWithValue("@File", rawData)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Congratulations PDF file is saved!", "HORE", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        conn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

That is exactly what I've been saying all along. I haven't run the code but, as far as I can see, it should do exactly what you want. If it doesn't then explain EXACTLY what does happen and we can address that specifically. Talking about a NullReferenceException over and over without ever telling us where it is thrown is of little value.

That said, if you do get a NullReferenceException hen the first thing to do is to determine which reference is null and work backwards to see where you expected it to be set and then work out why it either wasn't set or was cleared afterwards. That's where debugging comes in. If you don't know how to debug, i.e. set breakpoints and step through code, then learn now, starting here. Also, check out this thread to learn about dealing with NullReferenceExceptions specifically.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Thank you so much, even though I still face an error but now I know and really clear about `Filestream` or `File.ReadAllBytes` that you're talking about and `NullRefferenceException` exactly where line it's come from. Glad to learn new thing from you :) – eleonora anggi Jul 13 '18 at 02:26