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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:29:51+00:00 2026-06-09T11:29:51+00:00

I created a form that load an image then save it into a database.

  • 0

I created a form that load an image then save it into a database.

I want to save the value NULL into the database if the user doesn’t select any image.

I tried this code:

drow[1] = string.IsNullOrEmpty(imgData.ToString()) ? DBNull.Value : (object)imgData;

but it gives me this error:

Object reference not set to an instance of an object

this is the code I used to load the image :

private void simpleButton5_Click_1(object sender, EventArgs e)
{
    try
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            picture.ImageLocation = openFileDialog1.FileName;

            imgData = File.ReadAllBytes(openFileDialog1.FileName);
        }
    }
    catch (Exception ex)
    {
        // Could not load the image - probably related to Windows file system permissions.
        XtraMessageBox.Show(String.Format("Cannot display the image.\n You may not have permission to read the file, or it may be corrupt.\n\nReported error: {0}", ex.Message));
    }
}
  • 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-09T11:29:53+00:00Added an answer on June 9, 2026 at 11:29 am

    You are doing some unnecessary things in that line. There is no need to use ToString() and it is in fact harmful, as you will get a NullReferenceException if imgData is null. Just compare the value directly with null.

    This would be better, as well as immune to that NullReferenceException:

    drow[1] = imgData == null ? DBNull.Value : (object)imgData;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a form that stores free text fields into a MySQL database. All
I have created a form that contains fields used to accept a floating value
I have a very generic login form created that allows a user to access
I have a form that I created that is supposed to load login information
Greetings! I've created an APSX web form that returns a remote image based on
I created a form that overlays on top of another form in an alpha
I have created a form that is used for both adding and editing a
I've created a form that enters comments onto my blog posts but when the
I have created a list form that gets attached to a main form in
I've a custom form (created with form API) that need send an uploaded file

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.