Solved

Exception related to the image file

Posted on 2013-12-06
5
322 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

895 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