Click here to Skip to main content
16,016,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<asp:FileUpload ID=FileUploadControl1; runat=server;>
i want to save image in ms access database. image is stored as oleobject.
how to write update statement to insert image using where clause


C#
<string sql="insert" into="" values="" where="" username="?;&lt;br" mode="hold" />
using (OleDbCommand command = new OleDbCommand(SqlString, conn))
{
  command.CommandType = CommandType.Text;
  command.Parameters.AddWithValue("PhotoImage", Nattxt.Text);
  command.Parameters.AddWithValue("SignIamge", apldposttxt.Text);
  command.Parameters.AddWithValue("UserName", usertxt.Text);
  command.ExecuteNonQuery();
}
Posted
Updated 31-May-14 5:02am
v2
Comments
[no name] 31-May-14 10:54am    
http://www.w3schools.com/sql/sql_update.asp
DamithSL 31-May-14 11:07am    
you need update or insert!
Ashwini Thakare 31-May-14 12:20pm    
insert two images in a particular row by using where username = textbox value
the table already have username filled in it
DamithSL 31-May-14 12:33pm    
then you need update SQL statement, like below
update MyTable set image1=@image1, image2=@image2 where username=@username

1 solution

if you need to insert new record; use insert SQL statement[^]
if you need to update existing record; use update SQL statement[^]
below is sample code which insert image data to access database using OleDbConnection
C#
protected void btnSaveImage_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        string name = FileUpload1.PostedFile.FileName;
        int length = FileUpload1.PostedFile.ContentLength;
 
        byte[] imageBytes = new byte[length];
        Stream imageStream = FileUpload1.PostedFile.InputStream;
        imageStream.Read(imageBytes, 0, length);
 
        string connString = ConfigurationManager.ConnectionStrings["ImagesDB"].ConnectionString;
       OleDbConnection connection = new OleDbConnection(connString);
 
       string insertQuery = "INSERT INTO Images(ImageName, ImageSize, ImageData) VALUES(@ImageName, @ImageSize, @ImageData)";
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        command.CommandText = insertQuery;
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@ImageName", name);
        command.Parameters.AddWithValue("@ImageSize", length);
       command.Parameters.AddWithValue("@ImageData", imageBytes);
 
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
            lblMessage.Text = "Image data saved successfully";
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Unable to save image data";
        }
        finally
        {
            connection.Close();
        }
    }
}


Refer this article [^]
 
Share this answer
 
v2
Comments
Ashwini Thakare 31-May-14 12:52pm    
but i have used oleobject as datatype of image in ms access database
Ashwini Thakare 31-May-14 13:22pm    
i tried this code but my image does not got saved in the database
i have image datatype as oleobject

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900