Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exception related to the image file

Posted on 2013-12-06
5
Medium Priority
?
354 Views
Last Modified: 2013-12-12
Hi,
I get this exception
Server Error in '/App18_devst' Application.

Buffer cannot be null.
Parameter name: buffer
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

 Exception Details: System.ArgumentNullException: Buffer cannot be null.
Parameter name: buffer

Source Error:


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:



[ArgumentNullException: Buffer cannot be null.
Parameter name: buffer]
   System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable) +14257217
   App18._Default.refresh_img() +434
   App18._Default.save_click(Object sender, EventArgs e) +11299
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +155
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3804

 


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408  

and here is the relevant event.

        protected void refresh_img()
        {
            OleDbConnection conn = null;
            conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Jet OLEDB:Database Password=?????;Data Source=C:\inetpub\VS2012\DB1.accdb");
            try
            {
                conn.Open();
                OleDbCommand cmd = new OleDbCommand("SELECT [photo_file] FROM [ite_obj] where [ite_id]=@par_id", conn);
                if (tb_iteabbr.Text!="")
                {
                    cmd.Parameters.Add("@par_id", OleDbType.VarChar).Value = lb_iteid.Text.Trim();
                }
                else
                {
                    cmd.Parameters.Add("@par_id", OleDbType.VarChar).Value = "";
                }
                byte[] bArray = (byte[])cmd.ExecuteScalar();
                MemoryStream fs = new MemoryStream(bArray);
                System.Drawing.Image img = new System.Drawing.Bitmap(fs);
                img.Save(Path.Combine(Server.MapPath("~/"), "output" + Session["counter"].ToString() + ".jpg"));
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                ...
            }
            finally
            {
                conn.Close();
            }
        }

Open in new window

what is the reason of this exception?
0
Comment
Question by:HuaMinChen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39702544
Your database call must be returning null. I would encourage you to write defensive code which checks for these sort of error conditions.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39702549
But I can see there is "Long Binary Data" in the relevant table. Does it mean there is image file stored inside, or not?

If the error is due to empty file image, how to adjust the above codes?
t614.png
0
 
LVL 21

Expert Comment

by:Dale Burrell
ID: 39702575
You need to debug your code and find out the cause of the error, is the query wrong for example (most likely)? Or is it returning a different datatype. Or is the database connection wrong.

You should always check for a null returned value from the database. And then write it to an error log, and show the user a friendly "error" screen.
0
 
LVL 11

Author Comment

by:HuaMinChen
ID: 39702629
The codes were previously working fine.
The point is, how to adjust those for tackling with empty image case.
0
 
LVL 21

Accepted Solution

by:
Dale Burrell earned 480 total points
ID: 39702676
Do you mean something like this? Its pretty trivial so I'm not sure what specific help you need.

object bArray = cmd.ExecuteScalar();
if (bArray != null && bArray != System.DBNull.Value)
{
    MemoryStream fs = new MemoryStream((byte[])bArray);
    // etc
}
else
{
    // Some error handling, message to the user, whatever
}

Open in new window

0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question