Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET
How can i update an ole object field in ms access 2007 from c#.net?
Following is the code i have written but is giving me error
'No Value given for one or more parameters'
 

FileStream fs = default(FileStream);
fs = new FileStream(imgName, FileMode.Open, FileAccess.Read);
byte[] picByte = new byte[fs.Length];
fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
 
string strSQL = "Update Questions Set QuestionImg = @Img Where QueId = " + RecId;
OleDbCommand cmd = new OleDbCommand(strSQL, new OleDbConnection(conn));
 
OleDbParameter imgParam = new OleDbParameter();
imgParam.OleDbType = OleDbType.Binary;
imgParam.ParameterName = "Img";
imgParam.Value = picByte;
cmd.Parameters.Add(imgParam);
 
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
 
cmd.Dispose();
Posted 26-Feb-13 15:38pm
Edited 26-Feb-13 16:18pm
v3
Comments
e9net at 27-Feb-13 2:47am
   
change
string strSQL = "Update Questions Set QuestionImg = @Img Where QueId = " + RecId;
to
string strSQL = "Update Questions Set QuestionImg = @Img Where QueId = " + RecId & ";"
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Function Update(ByVal RecId As Integer, ByVal Imge As Byte()) As Boolean
        Dim Ret As Boolean = True
        Using oCn As New OleDb.OleDbConnection(StrConnection)
            Try
                oCn.Open()
                Using oCmd As New OleDb.OleDbCommand()
                    oCmd.Connection = oCn
                    oCmd.CommandText = "Update Questions Set QuestionImg = @Img Where QueId = " + RecId & ";"
                    oCmd.CommandType = CommandType.Text
 
                    oCmd.Parameters.Add(New OleDb.OleDbParameter("@Img", OleDb.OleDbType.Binary, 2147483647, ParameterDirection.Input, True, 0, 0, "", DataRowVersion.Proposed, IIf(IsNothing(Imge), DBNull.Value, Imge)))
 

                    oCmd.ExecuteNonQuery()
 
                End Using
 
            Catch ex As Exception
                Throw New Exception("_TBIMAGE::Update::Error Occured.", ex)
                Ret = False
            End Try
        End Using
        Return Ret
 
    End Function
 
  Permalink  
Comments
mistryshailesh at 27-Feb-13 12:59pm
   
Thanks for the solution. Code i working fine. but, i m not able to retrieve imgae value in picturebox again
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

PictureBox1.Image = GetBitmapFromByte("ImageFieldName")
 
   Function GetBitmapFromByte(ImgRecord As Byte) As Bitmap
       Dim _MyImage As Bitmap
       If (Not IsNothing(ImgRecord)) Then
           Using ms As New IO.MemoryStream(ImgRecord)
               _MyImage = New Bitmap(ms)
               ms.Close()
           End Using
       Else
           _MyImage = Nothing
       End If
       Return _MyImage
   End Function
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 28 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100