Solved

Exception related to the image file

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

630 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