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 7889845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:12:07+00:00 2026-06-03T06:12:07+00:00

there is the code for loading image from database and shows it in picture

  • 0

there is the code for loading image from database and shows it in picture box. the problem is if there’s no pic in a row it will encounter an error however i marked as allow nulls image column in database.

private void button1_Click(object sender, EventArgs e)
{
    sql = new SqlConnection(@"Data Source=PC-PC\PC;Initial Catalog=Test;Integrated Security=True");
    cmd = new SqlCommand();
    cmd.Connection = sql;
    cmd.CommandText = ("select Image from Entry where EntryID =@EntryID");
    cmd.Parameters.AddWithValue("@EntryID", Convert.ToInt32(textBox1.Text));
    var da = new SqlDataAdapter(cmd);
    var ds = new DataSet();
    da.Fill(ds, "Images");
    int count = ds.Tables["Images"].Rows.Count;

    if (count > 0)
    { 
    var data = (Byte[])(ds.Tables["Images"].Rows[count - 1]["Image"]);
    var stream = new MemoryStream(data);
    pictureBox1.Image= Image.FromStream(sream);
    } 
}
  • 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-03T06:12:08+00:00Added an answer on June 3, 2026 at 6:12 am

    you should check for DbNull.Value.

    This should do the trick:

    if (count > 0)
    { 
           if (ds.Tables["Images"].Rows[count - 1]["Image"] != DbNull.Value){
              var data = (Byte[])(ds.Tables["Images"].Rows[count - 1]["Image"]);
              (MemoryStreamstream = new MemoryStream(data)
               pictureBox1.Image= Image.FromStream(sream);
           }
     } 
    

    You marked column id DB to accept null value so DB handles it but you also need do handle null in your application (you cannot cast DbNull value).

    Hint

    While using any kind of Stream you should use Using statement to dispose Stream after usage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there code in VBA I can wrap a function with that will let
There is code that I use to pass new data to Database (firebird): FbConnection
I use below code to load image from URL in my winodws phone 7
I have this code that pulls an image from an url and displays it
I have following code on my site: backgroundImages[bg_img_path_b]=new Image(); backgroundImages[bg_img_path_b].src = bg_img_path_b; backgroundImages[bg_img_path_b].loaded=loading; //jQuery(backgroundImages[lastImage]).unbind('load.backgroundImages');
Loading the image one time works as done in the following steps (for code
I'm loading an image file from a web server, and then saving it to
i m loading an image from an url and displaying it in imageView .i
I am loading a image from the web to the local android phone. The
I'm loading a List<Image> from a folder of about 250 images. I did a

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.