Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 9th, 2012, 03:27 AM
Registered User
 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default why does itemcommand fires on page refresh or reload

i am using a Datalist to display products on my page .... and i am using item command to store value of corresponding row using link button inside datalist .. once i click addtocart button .. the corresponding item information store in database , but the same event fires happens again even if i click refresh button of my browser .. that is the value again enter in database even on refreshing the page

here is my code of itemcommand:

public void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionSt rings["eshopConnectionString"].ConnectionString);

//ShoppingCartID is value of session that i have created when user login
UserId = shoppingCartId;
conn.Open();
string prodname = ((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l1")).Text;
string produrl = ((Image)DataList1.Items[e.Item.ItemIndex].FindControl("sh1")).ImageUrl.ToString();
int prodprice = int.Parse(((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l2")).Text);
string productid = ((Label)DataList1.Items[e.Item.ItemIndex].FindControl("l3")).Text;
SqlCommand cmd1 = new SqlCommand(("select * from cartnew where productid ='" + productid + "'and userid='" + UserId + "' "), conn);
DataSet ds1 = new DataSet();
SqlDataAdapter adp1 = new SqlDataAdapter();
adp1.SelectCommand = cmd1;
adp1.Fill(ds1);
int k = ds1.Tables[0].Rows.Count;
if (k == 0)
{
if (IsPostBack)
{

//InsertData is a function to insert value of item if it doesnt exist in table
InsertData("insert into cartnew(Prodname,prodprice,produrl,userid,quantity ,productid)values('" + prodname + "','" + prodprice + "','" + produrl + "','" + UserId + "','" + 1 + "','" + productid + "')");
}
}

//to update quantity if item already exist in table

else
{
SqlCommand cmd2 = new SqlCommand("select * from cartnew where productid ='" + productid + "'and userid='" + UserId + "'", conn);
SqlDataAdapter adp = new SqlDataAdapter();
adp.SelectCommand = cmd2;
DataSet ds = new DataSet();
adp.Fill(ds);
int val = int.Parse(ds.Tables[0].Rows[0]["quantity"].ToString());

string s1 = ds.Tables[0].Rows[0]["productid"].ToString();
string s2 = ds.Tables[0].Rows[0]["userid"].ToString();

val = val + 1;
SqlCommand cmd3 = new SqlCommand("update cartnew set quantity='" + val + "' where productid ='" + s1 + "'and userid='" + s2 + "'", conn);
cmd3.ExecuteNonQuery();
}
 
Old June 9th, 2012, 04:54 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

A refresh in the browser repeats the last action. If that was a post, the post will be executed again and the code at the server will insert / update your data again.

The solution is a pattern called PRG: Post Redirect Get, where you redirect to the same page after the data has been submitted. For more details: http://en.wikipedia.org/wiki/Post/Redirect/Get

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 9th, 2012, 11:49 PM
Registered User
 
Join Date: Jun 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks sir for reply ,, i got answer my answer to prevent refresh of button .. iit sloved my problem ..





Similar Threads
Thread Thread Starter Forum Replies Last Post
reload parent page from included page sbkumar Javascript 2 July 24th, 2008 03:56 PM
Reload page Tooltip not work - Reload xslt file? ismailc XSLT 10 January 22nd, 2008 10:03 AM
reload/refresh problem notChavez PHP How-To 23 January 16th, 2007 06:21 AM
DataList ItemCommand event problem in ASP.NET page naidukap ASP.NET 1.0 and 1.1 Professional 0 December 26th, 2005 03:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.