Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
im using handlers to do upload edit delete images its work just fine ! but when i delete and set a defualt image dosnot show up until i refresh the page !

What I have tried:

C#
public class Handler1 : IHttpHandler, IRequiresSessionState
    {
        public static string strCon = System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
        public void ProcessRequest(HttpContext context)
        {
            SqlConnection _con = new SqlConnection(strCon);
            var id = context.Request.Params.GetValues("id");
            int ImgNumber = Convert.ToInt32(id[0].ToString());
            int wasteCheckID = Convert.ToInt32(context.Session["WasteCheckID"]);
            string stDelete = "DELETE FROM tbl_LogisticsWasteCheckImg WHERE WasteCheckID=" + wasteCheckID + " AND ImgNumber=" + ImgNumber;
            SqlCommand cmdDelete = new SqlCommand(stDelete, _con);
            _con.Open();
            cmdDelete.ExecuteNonQuery();
            _con.Close();
            string strget = "select img from tbl_logisticswastecheckimg where wastecheckid=114  and imgnumber=1";
            _con.Open();
            using (SqlDataAdapter sda = new SqlDataAdapter(strget, _con))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    byte[] bytes = (byte[])dt.Rows[0]["img"];
                    context.Response.BinaryWrite(bytes);

                    context.Response.End();
                }
                else
                {
                    context.Response.WriteFile("~/images/a.png");
                    //read image file into image object.
                    Image img = Image.FromFile(context.Server.MapPath("~/images/a.png"));

                    //imageconverter class convert image object to byte array.
                    byte[] bytes = (byte[])(new ImageConverter()).ConvertTo(img, typeof(byte[]));
                    context.Response.BinaryWrite(bytes);
                    context.Response.End();
                }
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
Posted
Updated 14-Oct-18 20:43pm
v2
Comments
Richard Deeming 11-Oct-18 9:01am    
"DELETE FROM tbl_LogisticsWasteCheckImg WHERE WasteCheckID=" + wasteCheckID + " AND ImgNumber=" + ImgNumber

Don't do it like that!

In this specific case, since both parameters are integers, you should be OK. But using string concatenation to build a SQL query can and will lead to SQL Injection vulnerabilities. Always use a parameterized query instead.
const string stDelete = "DELETE FROM tbl_LogisticsWasteCheckImg WHERE WasteCheckID = @WasteCheckID AND ImgNumber = @ImgNumber";
using (SqlCommand cmdDelete = new SqlCommand(strDelete, _con))
{
    cmdDelete.Parameters.AddWithValue("@WasteCheckID", wasteCheckID);
    cmdDelete.Parameters.AddWithValue("@ImgNumber", ImgNumber);
    
    _con.Open();
    cmdDelete.ExecuteNonQuery();
    _con.Close();
}

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]
Member 13990122 15-Oct-18 2:40am    
hi
Thanks , yes i know a bit about it :) but thanks agian ,

That's probably because the browser is caching the image. If you want to make sure the browser refreshes the image you can either:

(1) Add the appropriate Cache headers.
C#
HttpResponse Response = HttpContext.Current.Response;
Response.Expires = 0;
Response.Cache.SetCacheability(HttpCacheability.NoCache);

(2) Add a unique query string to the URL to force a refresh.
C#
Image1.ImageUrl = "~/images/a.png"+"?" + DateTime.Now.Ticks;
 
Share this answer
 
Comments
Member 13990122 15-Oct-18 2:43am    
hi ,
i tried to clear the cache, but the same problem ! add unique query string is hard due i send the ID as static.
. i call image bumber 1 !
Member 13990122 15-Oct-18 2:47am    
i have the code in the comment after.
Vincent Maverick Durano 15-Oct-18 10:49am    
You could still use the static ID. The idea is to append the DateTime.Now.Ticks to to the URL so it will force a browser refresh.
<pre><img id="Img1" runat="server" src='~/Get.ashx?imgID=1' class="img" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900