
How to avoid duplicate entry of a record in database when user hits refresh.
Problem
In case we are saving data in database on click of a button, and what happens if a user hits refresh??
The data will be saved twice in the database. How can we avoid this behavior??
Solution
Here is the solution in case the website is being developed in asp.net version 1.1 + with C# as the scripting language. The below implementation is designed by keeping “Order of page lifecycle events” in mind. The event order is Page_Load => btnSubmit_Click => Page_PreRender. The following code can be implemented to avoid the problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session[“RefrechCheck”] = DateTime.Now.ToString();
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
ViewState[“RefrechCheck”] = Session[“RefrechCheck”];
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid && Session[“RefrechCheck”].ToString() == ViewState[“RefrechCheck”].ToString())
{
Session[“RefrechCheck”] = DateTime.Now.ToString();
int result = SaveComment();
}
}
Happy coding.more
Categories
- Android App Development6
- App Store Optimization2
- Apple Watch App Development8
- Asp Dot Net Boilerplate1
- ASP Dot Net Development26
- Asp Dot Net Zero1
- CakePHP Development1
- CMS Development6
- Codeigniter Development2
- CPANEL4
- CRM development2
- CRM Development India1
- Cross Platform App Development6
- Dedicated Development Centre1
- Dot Net Development28
- Drupal Web Development2
- Ecommerce Development13
- Google Panda 4.21
- Internet Marketing SEO6
- iOS App Development9
- iPhone App Development11
- Joomla Development3
- Mobile Application Development30
- My SQL Server3
- News62
- Open Source Development10
- PHP Development25
- Search Engine Optimization33
- Search Engine Optimization Service India20
- SEO India21
- Sharepoint Development2
- Social Media Optimization2
- Software Development11
- Swift Programming Language1
- Technology11
- Uncategorized12
- Web Design and Development20
- WordPress Customization8
- WordPress Development11

Advantages of ASP.NET Development
