Image insert time showing error msaccess and vb.net

  • Thread starter Thread starter Cnivas K
  • Start date Start date
C

Cnivas K

Guest
Imports System.IO
Imports System.Data.OleDb
Public Class Form1
Dim dt As DataTable
Dim cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\service.accdb;")
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytImage() As Byte
Dim cmd As New OleDbCommand
Dim strimage As String
Dim myms As New IO.MemoryStream
If Not IsNothing(Me.PictureBox1.Image) Then
Me.PictureBox1.Image.Save(myms, Me.PictureBox1.Image.RawFormat)
bytImage = myms.GetBuffer
strimage = " ? "
Else
bytImage = Nothing
strimage = " Null "
End If
cmd.Connection = cn
cmd.CommandText = "insert into stddata (stdid,name,photo) values (" & Me.TextBox1.Text & ",’" & Me.TextBox2.Text & "’, " & strimage & ")"
cn.Open()
If strimage = " ? " Then
cmd.Parameters.Add(strimage, OleDb.OleDbType.Binary).Value = bytImage
End If
cmd.ExecuteNonQuery()
MsgBox("data save succe")
cn.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PictureBox1.Image = Nothing
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim bytImage() As Byte
Dim cmd As New OleDbCommand
' Dim strimage As String
Dim da As New OleDbDataAdapter

Dim myms As New IO.MemoryStream
da.SelectCommand = New OleDbCommand("select * from studata where (stdid = '" & TextBox1.Text & "') ")
da.SelectCommand.Connection = cn
cn.Open()
da.Fill(dt)
If dt.Rows.Count > 0 Then
TextBox1.Text = dt.Rows(0).Item("name") & ""
If Not IsDBNull(dt.Rows(0).Item("photo")) Then
bytImage = dt.Rows(0).Item("photo")
For Each ar As Byte In bytImage
myms.WriteByte(ar)
Next
PictureBox1.Image = System.Drawing.Image.FromStream(myms)
End If
Else
MsgBox("Record Not Found")
End If
End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
If OpenFileDialog1.ShowDialog = 1 Then
Me.PictureBox1.Image = System.Drawing.Image.FromFile(OpenFileDialog1.FileName)

End If
End Sub
End Class

Continue reading...
 
Back
Top