I am getting the error:
Object reference not set to an instance of an object.
This appears when I'm trying to upload any file to my database. This is a project I am developing to deliver for my school (the final exam)
Here's my code:
Try
OpenFileDialog.Filter = "All files (*.*)|*.*| JPEG FILES(*.jpg)|*.jpg| RAW FILES (*.raw)|*.raw| WORD FILES (*.doc)|*.doc| WORD FILES (*.docx)|*.docx| EXCEL FILES (*.xls)|*.xls| EXCEL Files (*.xlsx)|*.xlsx| PNG Files (*.png)|*.png| GIF FILES (*.gif)|*.gif| PDF FILES (*.pdf)|*.pdf| RAR Files (*.rar)|*.rar| AUTOCAD FILES (*.dwg)|*.dwg| EXE FILES (*.exe)|*.exe| ZIP FILES (*.zip)|*.zip| TXT FILES (*.txt)|*.txt"
OpenFileDialog.FilterIndex = 1
OpenFileDialog.Title = "Open File"
OpenFileDialog.Multiselect = True
obj_openfile = OpenFileDialog
'Show The Dialog
If obj_openfile.ShowDialog = Windows.Forms.DialogResult.OK Then
in_file = obj_openfile.FileName.ToString
Dim fich As String = obj_openfile.SafeFileName
obj_openfile.Dispose()
obj_openfile = Nothing
con.Close()
con.ConnectionString = "Data Source=IZZ;Initial Catalog=regteste;Integrated Security=True"
con.Open()
'Converter to bytesream
Dim fs As FileStream
fs = New FileStream(in_file, FileMode.Open, FileAccess.Read)
Dim docByte As Byte() = New Byte(fs.Length - 1) {}
fs.Read(docByte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
Dim filebyte As Byte() = Nothing
filebyte = System.IO.File.ReadAllBytes(obj_openfile.FileName)
cmd = New SqlCommand("INSERT INTO Filestelefones_pt (Name,Data) VALUES (@Name, @Data)", con)
cmd.Parameters.AddWithValue("@Name", fich)
cmd.Parameters.AddWithValue("@Data", docByte)
cmd.ExecuteNonQuery()
con.Close()
con.Open()
txtabrir.Text = in_file
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub