Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

In Page_Load, I am assigning an image to:

XML
<asp:image id="imgPhoto" runat="server" xmlns:asp="#unknown"></asp:image>


Here is my code:

page_load
============

C#
string photo = siteurl + DBUtilities.ExecuteScalar(DBConnections.GetSQLConnectionString(), CommandType.Text, "select photo from visitors_images where visitor_id='" + Request.QueryString["visitor_no"].ToString() + "'").ToString();
            if (IsExists(photo))
                imgPhoto.ImageUrl = photo;
            else
                imgPhoto.ImageUrl = "~/includes/images/no_person.jpg";


Submit_button
=============

C#
fulPhoto.SaveAs(Server.MapPath("~/includes/visitors/" + IMGNAME));
DBUtilities.ExecuteNonQuery(DBConnections.GetSQLConnectionString(), CommandType.Text, "UPDATE visitors_images SET photo='" + IMGNAME + "' WHERE visitor_id='" + Request.QueryString["visitor_no"].ToString() + "'");
Response.Redirect(Request.RawUrl.ToString());


But, if I refresh manually, it shows the latest photo. Can you please give some suggestion.

Thanks!
Posted
Updated 23-Aug-10 8:28am
v5
Comments
Ankur\m/ 23-Aug-10 5:56am    
I tried improving your question but it seems you didn't write the 'image button' part correctly. Please modify it.

Well, you should perhaps read a book on ASP.NET, and know a bit about your subject before you call yourself 'hire me for your work'. Your issues are legion, your code is a disaster, and I could easy erase your database because your site is insecure and poorly written.

Your core issue is that you don't understand the ASP.NET page lifecycle. Do you know how to use the debugger ? If you did, and you set some breakpoints, you'd notice that page load runs BEFORE your button event, so it loads the OLD photo, then your event changes the photo, so the next time you refresh, you'll see the new one. If you can't write this code properly ( and if you cannot, I hope no poor sucker has 'hired you' to write it ), then at least move it to page prerender, and it will work fine.
 
Share this answer
 
Comments
Dalek Dave 23-Aug-10 6:32am    
Don't hold back, tell it as it is! :)
ntrraorao 23-Aug-10 7:53am    
Yeah, I know. this is not my site. I am just doing some demo work. and I know how do write better code with Stored procedures to avoid sql injections.
hello,

I agree with the post above, the current code is open to SQL Injection.[^]

Here is a quick solution to bypass the browser cache, try append a random number in the query string. Make sure you fix the problems mentioned by Christian.

MIDL
Random r = new Random();
        int RandomNumber = r.Next();


Response.Redirect(Request.RawUrl.ToString()+"?r=" + RandomNumber.ToString();
 
Share this answer
 
Comments
ntrraorao 23-Aug-10 7:51am    
No, this is idea is not working for me. still showing old photo.
Why dont you set the

imgPhoto.ImageUrl

after you upload the image.
Means after you do
fulPhoto.SaveAs(Server.MapPath("~/includes/visitors/" + IMGNAME));

This will make sure that the proper image link is transferred to the control.

If there is a full postback, and you save the file synchronously, the image should be displayed. Dont use any thread to save the file.
 
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