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

  • Home
  • SEARCH
  • 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 8378973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:03:21+00:00 2026-06-09T16:03:21+00:00

what I’ve been trying to find out is how I can store the BLOB

  • 0

what I’ve been trying to find out is how I can store the BLOB of an image into my database without saving it to the filesystem first, so directly from the server’s memory.

I use an sql server and among other form information I have 2 images that need to be stored in the database. I would also like to know how I can read them out and convert them back to images.

In the db I have “Thumbnail” which is of type “image”. That should be correct if I’m not wrong.

For the image upload I use the following asp control:

<asp:FileUpload ID="_imageUpload" runat="server" />

I have never done anything like this as I am quite new to working with databases especially together with websites.

Oh, and sorry if this question has been asked and answered already.

Thanks in advance!

[edit]

My entire code:

protected void _uploadImageBtn_Click(object sender, EventArgs e)
{
    string extension;

    // checks if file exists
    if (!_imageUpload.HasFile)
    {
        _resultLbl.Text = "Please, Select a File!";
        return;
    }

    // checks file extension
    extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();

    if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png"))
    {
        _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed.";
        return;
    }

    // checks if image dimensions are valid
    if (!ValidateFileDimensions(140, 152))
    {
        _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px.";
        return;
    }

    int fileLen;
    string displayString = "";

    // Get the length of the file.
    fileLen = _imageUpload.PostedFile.ContentLength;

    // Create a byte array to hold the contents of the file.
    byte[] input = new byte[fileLen - 1];
    input = _imageUpload.FileBytes;

    // Copy the byte array to a string.
    for (int loop1 = 0; loop1 < fileLen; loop1++)
    {
        displayString = displayString + input[loop1].ToString();
    }

    try
    {

        SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial Catalog=database;User ID=user;Password=pw");

        string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";

        SqlCommand sqlCom = new SqlCommand(qry, sqlCn);

        sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input;

        sqlCn.Open();
        sqlCom.ExecuteNonQuery();
        sqlCn.Close();

    }

    catch (Exception)
    {
        (...)
    }
}

public bool ValidateFileDimensions(int aHeight, int aWidth)
{
    using (System.Drawing.Image image = System.Drawing.Image.FromStream(_imageUpload.PostedFile.InputStream))
    {
        return (image.Height == aHeight && image.Width == aWidth);
    }
}
  • 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-06-09T16:03:22+00:00Added an answer on June 9, 2026 at 4:03 pm

    You can save the returned byte array from FileUpload.FileBytes.

    if(_imageUpload.HasFile)
    {
     byte[] imageData = _imageUpload.FileBytes;
    
     using(SqlConnection sqlCn = new SqlConnection("Server=localhost;database=databaseName;uid=userName;pwd=password"))
      {
        string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";
        using(SqlCommand sqlCom = new SqlCommand(qry, sqlCn))
         {
           sqlCom.Parameters.Add("@thumbnail",
                                  SqlDbType.Image,
                                  imageData.Length).Value=imageData;
           sqlCn.Open();
           sqlCom.ExecuteNonQuery();
           sqlCn.Close();
         }
       }
    }
    

    EDIT:

    protected void _uploadImageBtn_Click(object sender, EventArgs e)
    {
        string extension;
    
        // checks if file exists
        if (!_imageUpload.HasFile)
        {
            _resultLbl.Text = "Please, Select a File!";
            return;
        }
    
        // checks file extension
        extension = System.IO.Path.GetExtension(_imageUpload.FileName).ToLower();
    
        if (!extension.Equals(".jpg") && !extension.Equals(".jpeg") && !extension.Equals(".png"))
        {
            _resultLbl.Text = "Only image files (.JPGs and .PNGs) are allowed.";
            return;
        }
    
        // checks if image dimensions are valid
        if (!ValidateFileDimensions(140, 152))
        {
            _resultLbl.Text = "Maximum allowed dimensions are: width 1520px and height <= 140px.";
            return;
        }
    
    
        byte []input = _imageUpload.FileBytes;
        SqlConnection sqlCn = new SqlConnection("Data Source=localhost;Initial 
                                     Catalog=database;User ID=user;Password=pw");
    
        string qry = "INSERT INTO Project (thumbnail) VALUES (@thumbnail)";
        SqlCommand sqlCom = new SqlCommand(qry, sqlCn);
        sqlCom.Parameters.Add("@thumbnail", SqlDbType.Image, input.Length).Value = input;
        sqlCn.Open();
        sqlCom.ExecuteNonQuery();
        sqlCn.Close();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

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.