Solved

Exception related to the image file

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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…
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…

759 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