Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:22:05+00:00 2026-05-28T20:22:05+00:00

My code for inserting image in database is as follows: MemoryStream ms =new MemoryStream();

  • 0

My code for inserting image in database is as follows:

MemoryStream ms =new MemoryStream();
byte[] PhotoByte=null;
PhotoByte=ms.ToArray();
pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
PhotoByte =ms.ToArray();
Str = "insert into Experimmm Values('" + PhotoByte + "','" + textBox1.Text + "')";
Conn.Open();
cmd.Connection = Conn;
cmd.CommandText = Str;
cmd.ExecuteNonQuery();
Conn.Close();

Which is going well. I can see binary data in ma database table like <Binary Data>
My code for retrieving data is:

Str ="select * from Experimmm where id = '" +textBox2.Text + "'";
Conn.Open();
cmd.Connection = Conn;
cmd.CommandText = Str;
dr = cmd.ExecuteReader();
if (dr.Read())
{ label1.Text = dr.GetValue(1).ToString();
byte[] PhotoByte = (byte[])dr.GetValue(0);
MemoryStream mem = new MemoryStream(PhotoByte, 0, PhotoByte.Length);
//but an error takes place on next line "Parameter is not valid."             
pictureBox2.Image = Image.FromStream(mem);
} Conn.Close();

I’m using visual studio 10 , C# ,sql server 2005

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:22:06+00:00Added an answer on May 28, 2026 at 8:22 pm

    It is possible of course to store your images in db. However it is not recommended. It is better to store them in a file system.

    Your code is a bit messy. The following is a better example.

    MemoryStream ms =new MemoryStream(); 
    byte[] PhotoByte=null; 
    pictureBox1.Image.Save(ms, ImageFormat.Jpeg); 
    PhotoByte =ms.ToArray(); 
    // I'm not sure whether you are able to create an sql by simply concating the string
    Str = "insert into Experimmm Values('@PhotoBytes','@MyTextValue')"; 
    // You have to parametrize your query
    cmd.Parameters.AddWithValue("PhotoBytes", PhotoByte);
    // This also helps you to avoid syntactical corruption in case of ' and sql injection
    cmd.Parameters.AddWithValue("MyTextValue", textBox1.Text );
    Conn.Open(); 
    cmd.Connection = Conn; 
    cmd.CommandText = Str; 
    cmd.ExecuteNonQuery(); 
    Conn.Close(); 
    

    When you retrieve you could use a binary writer in some handler

    namespace TestNS
    {
       public class MyHttpHandler : IHttpHandler
       {
          // Override the ProcessRequest method.
          public void ProcessRequest(HttpContext context)
          {
             // Your preparations, i.e. querystring or something
             var conn = new SqlConnection("Your connectionstring");
             var command = new SqlCommand("Your sql for retrieval of the bytes", conn);
             conn.Open();
             var data = (Byte[])command.ExecuteScalar();
             conn.Close();
             context.Response.BinaryWrite(data);      }
    
          public Boolean IsReusable
          {
             get { return false; }
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem in php code inserting values into database (I use PHPMyAdmin).
I am inserting an Image in the MediaStore cache using the following code snippet:
i want to save and retrieve image path in my application this is my
I have some problem with inserting image when i generate pdf using FPDF library
I used this code for inserting records in a person table in my DB
Basically I am inserting an image using the listviews inserting event, trying to resize
I'm inserting an img tag into my document with the new Element constructor like
I am trying to edit this code so that instead of just inserting the
I'm having some trouble scaling an image that i am inserting. I must be
There's a gotcha when inserting img's dynamically via scripts. Take the following code to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.