Solved

Exception related to the image file

Posted on 2013-12-06
5
312 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
  • 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 10

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 10

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 160 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now