ASP.NET - Browser refresh problem....

Asked By Sneha Choudhary
23-Mar-09 04:20 AM

  Hi,

I know I am going to ask a silly question. But I am fighting with this problem for the last one week and unable to get the result. So please assist me.

I am building a shopping cart project. At a certain point there is a condition that whenever user enters the same product again and again, only the quantity should be updated. I have done that and is working fine.

But the problem is : Suppose I am having a single item in the cart with quantity "1". Whenever I click the browser refresh it automatically increases the quantity of the item.
That is whenever I refresh the page it will update the quantity.

I searched a lot and got the conclusion that:

When page posts, browser keeps the posted data in cache and sends it again when refresh button is pressed. So all the events executed before will get executed again. To avoid this, do a redirect once you added an item to shopping cart. Redirection will clear the post data.

// do shopping cart updation
Response.Redirect("confirmation.aspx")

In confirmation page, you can inform user that the quantity has been updated. Redirecting to same page will work as well.

But I am unable to understand where to apply the redirect.

 

1        protected void Page_Load(object sender, EventArgs e)
2 {
3 try
4 {
5 HyperLink HLink = (HyperLink)Master.FindControl("HyperLink3");
6 if (HLink != null)
7 {
8 HLink.Enabled = false;
9 }
10
11
12 if (!Page.IsPostBack)
13 {
14 if (Session["Cart"] == null)
15 {
16 Lblmsg.Visible = true;
17 dt = new DataTable();
18 dt.Columns.Add("REF", typeof(string));
19 dt.Columns.Add("Description", typeof(string));
20 dt.Columns.Add("QTY", typeof(int));
21 dt.Columns.Add("Price", typeof(float));
22 dt.Columns.Add("Cost", typeof(float));
23 get_data();
24 Session["Cart"] = dt;
25
26 }
27 else
28 {
29 Lblmsg.Visible = false;
30 dt = (DataTable)Session["Cart"];
31 get_data();
32 Session["Cart"] = dt;
33 }
34 }
35 }
36 catch(Exception ee)
37 {
38 Lblmsg.Text = ee.Message;
39 }
40 }
41
42 public void get_data()
43 {
44 try
45 {
46 pro_id = Request.QueryString["pr_id"];
47 quanti = Convert.ToInt32(Request.QueryString["quant"]);
48
49 if (pro_id != null)
50 {
51 SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted_Connection=yes");
52 SqlCommand cmd = new SqlCommand();
53 cmd.Connection = con;
54 cmd.CommandText = "select ICEMACHINES.product_id,ICEMACHINES.sh_desc,ICEMACHINES.price from ICEMACHINES where ICEMACHINES.product_id LIKE @product_id union select GLASSWARE.product_id,GLASSWARE.sh_desc,GLASSWARE.price from GLASSWARE where GLASSWARE.product_id LIKE @product_id ";
55 cmd.Parameters.Add("@product_id", SqlDbType.NVarChar, 50).Value = pro_id;
56 cmd.Connection.Open();
57 SqlDataReader rdr = cmd.ExecuteReader();
58 ArrayList arRole1 = new ArrayList();
59
60 while (rdr.Read())
61 {
62 desc = (rdr["sh_desc"]).ToString();
63 id = (rdr["product_id"]).ToString();
64 unitprice = Convert.ToSingle((rdr["price"]));
65 cost = unitprice * quanti;
66
67 }
68 cmd.Connection.Close();
69
70 int dtcount = dt.Rows.Count;
71 if (dtcount > 0)
72 {
73 string dc = dt.Columns[0].ColumnName;
74 string filter = String.Format("REF LIKE '{0}*'", id);
75 DataRow[] arrMatchingValues = dt.Select(filter, "REF");
76 if (arrMatchingValues.Length > 0)
77 {
78 DataRow drUpdateMe = arrMatchingValues[0];
79 int drUpdateQuant = Convert.ToInt32(drUpdateMe["QTY"]);
80 int quan = quanti + drUpdateQuant;
81 float cos = unitprice * quan;
82 drUpdateMe["QTY"] = quan;
83 drUpdateMe["Cost"] = cos;
84 dt.AcceptChanges();
85 Session["data"] = dt;
86 GridView1.DataSource = dt;
87 GridView1.DataBind();
88
89 }
90 else
91 {
92 DataRow myrow = dt.NewRow();
93 myrow["REF"] = id;
94 myrow["Description"] = desc;
95 myrow["QTY"] = quanti;
96 myrow["Price"] = unitprice;
97 myrow["Cost"] = cost;
98 dt.Rows.Add(myrow);
99 dt.AcceptChanges();
100 Session["data"] = dt;
101 GridView1.DataSource = dt;
102 GridView1.DataBind();
103 }
104 }
105 else
106 {
107 DataRow myrow = dt.NewRow();
108 myrow["REF"] = id;
109 myrow["Description"] = desc;
110 myrow["QTY"] = quanti;
111 myrow["Price"] = unitprice;
112 myrow["Cost"] = cost;
113 dt.Rows.Add(myrow);
114 dt.AcceptChanges();
115 Session["data"] = dt;
116 GridView1.DataSource = dt;
117 GridView1.DataBind();
118 }
119
120 }
121 else
122 {
123 int a = 1;
124 }
125 total_items = total_items + quanti;
126 total_Price = total_Price + cost;
127 Session["items"] = total_items;
128 Session["value"] = total_Price;
129 }
130 catch(Exception ee)
131 {
132 Lblmsg.Text = ee.Message;
133 }
134 }
  

 Please check it out and suggest me where to use redirect. I followed these links:

http://blog.andreloker.de/post/2008/06/Post-Redirect-Get.aspx

http://forums.asp.net/t/1120319.aspx


waiting for your response.


cheers
sneha

R::Browser refresh problem....  R::Browser refresh problem....

23-Mar-09 05:09 AM

Seeing through the articles mentioned, the solution to redirect is to have three step process...

Whenever you want to save any data, the updated quantity in your case, you need to save the data to the database and redirect to a different page to get the data...

Still you need to proceed to do any updations, then have a link in the get page to the update page and do the stuff there...

 

  Vasanthakumar D replied to Sneha Choudhary
23-Mar-09 11:36 AM

Hi,

you need to redirect after you store the values in sessions.... it may be in your save button..

pls post your full code....

Create New Account
help
using cache object of ASP.NET? (B) How can you cache different version of same page using ASP.NET cache object? (A) How will implement Page Fragment Caching? (B) Can you compare ASP.NET sessions with classic ASP? (B) Which are of using Query Strings? (I) What is Absolute and Sliding expiration? (I) What is cross page posting? 93 (I) How do we access viewstate value of this page in the next page ? (I) Can we post and access view state in another application? (I) What is SQL In which event are the controls fully loaded? (B) How can we identify that the Page is Post Back? (B) How does ASP.NET maintain state in between subsequent request? (A) What is event bubbling? B) How do we assign page specific attributes? (A) How do we ensure viewstate is not tampered? (B) What is the control are valid and proper? (A) If client side validation is enabled in your Web page, does that mean server side code is not run. (A)Which JavaScript file is referenced
true" ShowDeleteButton = "true" / > < / Columns> < / asp:GridView> .cs code- Function to bind Grid private void getdata() { SqlConnection con = new SqlConnection("Data Source = 10.0.2.8;Initial Catalog = JitendraDB;User ID = sa;password = change_123"); SqlDataAdapter RowIndex].Cells[1].Text; / / FOR GETTING ENPID string deleteQuery = "delete from emp where empid = ' " + strEmpId + " ' "; SqlConnection cn = new SqlConnection("CONNECTION STRING"); cn.Open(); SqlCommand cmd = new SqlCommand(deleteQuery, cn); cmd.ExecuteNonQuery(); cn.Close(); GridView1.EditIndex = -1; Response.Write("<script> alert('Record Deleted DropDeptId")).SelectedValue.ToString(); string UpdateQuery = "update emp set empname = '" + strEmpName + "' , dep = '" + strDeptId + "' where empid = ' " + strEmpId + " ' "; SqlConnection cn = new SqlConnection("CONNECTION STRING"); cn.Open(); SqlCommand cmd = new SqlCommand(UpdateQuery, cn); cmd.ExecuteNonQuery(); cn.Close(); GridView1
that image path from it, and display the image from that location in the web page. In order to do this, first we need to use ADO.NET to connect to explanation above is very simple. Collapse | Copy Code public partial class _Default : System.Web.UI.Page { SqlConnection conn = new SqlConnection(); protected void Page_Load( object sender, EventArgs e) { conn.ConnectionString = " Data Source = MyServer; Integrated Security = True System.Configuration; using System.Data.SqlClient; public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { SqlConnection con = new SqlConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings ["ConnectionString"].ConnectionString; / / Create SQL Command SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select ImageName, Image from Images" + " where ID = @ID"; cmd.CommandType = System.Data.CommandType
how to call stored procedure in asp.net page HI, i have written a stored procedure in sql server now i want to call the stored procedure from the asp.net page using c#. . i am facing some problem in doing that as i a begginner. . . can a stored procedure and the code that is calling the stored procedure in asp.net page. . and sending the values through it and storing it in the database. . can anyone help shortcode ) values ( @subjectid , @subjectname , @shortname , @userid , @shortcode ) end GO in c# write the following code SqlCommand cmd = new SqlCommand ( "subjectInsert" , con.OpenConnection()); / / here u should give the sqlconnection cmd.CommandType = CommandType .StoredProcedure; cmd.Parameters.AddWithValue( "@subjectid" , subjectid); cmd.Parameters.AddWithValue( "@subjectname" , txtsubject.Text else if @transtype = 'delete' begin - -your delete query here end ASPX Code. . . System.Data.SqlClient.SqlConnection Sqlcon = new System.Data.SqlClient.SqlConnection( "connection string" ); System.Data.SqlClient.SqlCommand Sqlcmd = new System.Data.SqlClient.SqlCommand(); Sqlcmd.CommandText = "" ; Sqlcmd
from Sql server 2005 using C#.NET in image control. i am buidling a web page in C#.net and used sql server as backend, i want to display in runtime an image in image control in aspx page in page_load event. every thing is going well and displaying no error but when i m trying to run this page (F5) image is not displaying in the control. myaccounts.aspx page - -- -- -- -- -- -- -- -- -- -- -- < asp : GridView ID = "gvImages" runat = "server" AllowPaging = "True" AutoGenerateColumns = "False" CellPadding = "4" ForeColor = "#333333" GridLines using System.Data.Sql; using System.Data.SqlClient; public partial class myaccounts : System.Web.UI. Page { protected void Page_Load( object sender, EventArgs e) { Label2.Text = Session[ "Name" ].ToString(); TextBox4.Text = "123123" ; string imageidA = Session[ "Pass" ].ToString(); string connectionString = ConfigurationManager .ConnectionStrings[ "connectbillbook" ].ConnectionString; SqlConnection datacon = new SqlConnection (connectionString SqlCommand comm = new SqlCommand ( "Select * from wall" , datacon); SqlCommand comm1 = new SqlCommand ( "Select username from signup