Updating Image file in database using MS Access - vb.net

Here is my code
Dim Cmd As New OleDbCommand
Dim strsql As String
Try
Dim fsreader As New FileStream(OpenFile.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
strsql = "UPDATE AccResult SET (PicFile, PicName) Values (#pfile, #pname) WHERE StudNo = '" & Form1.sNo.Text & "'"
Cmd = New OleDbCommand(strsql, con)
Cmd.Parameters.AddWithValue("#pfile", txtSave.Text)
Cmd.Parameters.AddWithValue("#pname", imgbuffer)
Cmd.ExecuteNonQuery()
Cmd.Dispose()
Catch ex As Exception
End Try
please help me how can i update the students data when saving their picture in database. im using VB.NET. Thank You.

Looks like simple error in your logic - you have the filename reversed with the file content
Cmd.Parameters.AddWithValue("#pfile", txtSave.Text)
Cmd.Parameters.AddWithValue("#pname", imgbuffer)
Should be
Cmd.Parameters.AddWithValue("#pfile", imgbuffer)
Cmd.Parameters.AddWithValue("#pname", txtSave.Text)

Related

Saving data to access not working, despite no errors

Hey I'm trying to use this code to save to my access database and although there are no errors and it says data saved, my access database doesn't receive the new data. What's wrong with it?
Private Sub savenew()
Dim conn As New OleDbConnection
conn = New OleDbConnection
dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbsource = "Data Source = FULL YUGIOH ACCESS DATABASE.accdb;"
conn.ConnectionString = dbprovider & dbsource
Dim reader As OleDbDataReader
Dim command As OleDbCommand
Try
conn.Open()
Dim query As String
query = "insert into item(name) values ('" & TextBox4.Text & "')"
command = New OleDbCommand(query, conn)
reader = command.ExecuteReader
MsgBox("data saved")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
(table in my database is called item, and column is called name.)

Save and Read RichText from Access file with Image is SLOW

If I use the following code to write and read rtf data into an access .mdf file its fast to save the data but very slow to read it back.
I tested with a rtf file which is 1mg in size contains around 10 lines in bold and 4 small images.
Is there anyway to read it back faster,
Note if load the rtf from file it takes around 1 sec,
SAVE DATA
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\paul\Documents\Code.mdb"
Using myconnection As New OleDbConnection(constring)
myconnection.Open()
Dim sqlQry As String = "INSERT INTO [table1] ([Code]) VALUES (#Code)"
Using cmd As New OleDbCommand(sqlQry, myconnection)
cmd.Parameters.AddWithValue("#code", RTB1.Rtf)
cmd.ExecuteNonQuery()
End Using
myconnection.Close()
End Using
READ DATA
Dim constring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\paul\Documents\Code.mdb"
Dim ds As New DataSet
Using myconnection As New OleDbConnection(constring)
myconnection.Open()
Dim strSql As String = "SELECT * FROM table1"
Using cmd As New OleDbCommand(strSql, myconnection)
Dim da As New OleDbDataAdapter(cmd)
da.Fill(ds, "Table")
End Using
myconnection.Close()
End Using
Me.RTB1.rtf = ds.Tables(0).Rows(0).Item(1)

couldnt save current locked by another user

When i trying to browse and choose some image into picture box and then i click update button
it's error message pop up and display couldnt be save current locked by another user.
If lblTable.Text = "Visitor_ApplyPass" Then
Dim fsr As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Using br As New BinaryReader(fsr)
Dim imgbuffer(fsr.Length) As Byte
br.Read(imgbuffer, 0, fsr.Length)
fsr.Close()
Using con As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=..\VisitorPass.accdb")
con.Open()
Dim sql As String
sql = "Update " & lblTable.Text & " set Visitor_Name='" & txtName.Text & "',[Visitor_Photo]= #imgfile where [VisitorApply_ID]=" & lblID2.Text & ""
Using cmd As New OleDbCommand(sql, con)
cmd.Parameters.AddWithValue("#imgfile", imgbuffer)
cmd.ExecuteNonQuery()
MsgBox("profile is updated")
con.Close()
End Using
End Using
End Using
Me.Close()

Load Report failed with crystal report

I got an error (load report failed) when trying to print crystal report in my vb.net application
but my problem is (this error hasn't occurred always) maybe after printing unknown number of reports, then once this error has occurred.
Here is my code:
Dim cmd As New OleDbCommand
Dim connp As New OleDbConnection
Dim da As New OleDbDataAdapter
Dim ds As New DataSet
Dim strsql As String
Dim strreprotname As String
Try
connp.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Application.StartupPath & "\report.mdb; Jet OLEDB:Database Password=KNOZ1003"
connp.Open()
ds.Reset()
strsql = "select * from tab3"
cmd.CommandText = strsql
cmd.Connection = connp
da.SelectCommand = cmd
da.Fill(ds)
strreprotname = "cashrpt"
Dim strreportpath As String = Application.StartupPath & "\Reports\" & strreprotname & ".rpt"
Dim rptdocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
Dim prnset As New Printing.PrinterSettings
Dim pg As New Printing.PageSettings
rptdocument.Load(strreportpath)
rptdocument.SetDataSource(ds.Tables(0))
rptdocument.SetDatabaseLogon("", "", "", Application.StartupPath + "\report.mdb")
prnset.PrinterName = cashprinter
rptdocument.PrintToPrinter(prnset, pg, False)
cmd.Dispose()
connp.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

empty path name is not legal (vb.net 2005 ,access 2000,)

[Error] empty path name is not legal
The problem is I cannot insert image to Access database. Which line is error. Thanks In advance who helping me
Regards,
Fizul
Dim OpenFileDialog1 As New OpenFileDialog
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
Dim breader As New IO.BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
cnn.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\db1.accdb;"
cnn.Open()
Dim sql As String
sql = "insert into Table1 Values(" & TextBox1.Text & ",'" & imgbuffer.Length & "')"
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
cmd.ExecuteNonQuery()
cmd.Dispose()
cnn.Close()
The problem is in these first two lines:
Dim OpenFileDialog1 As New OpenFileDialog
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
You created an instance of OpenFileDialog, but you never called it's ShowDialog method so the FileName property is Nothing. You need something like this:
Using OpenFileDialog1 As New OpenFileDialog
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim fsreader As New IO.FileStream(OpenFileDialog1.FileName,IO.FileMode.Open, IO.FileAccess.Read)
'remaining of code here
End If
End Using
The Using statement makes sure that the dialog is disposed correctly.