
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 Development Company7
- Angular Development18
- AngularJs Development1
- App Store Optimization22
- Apple Watch App Development17
- ASP.NET Boilerplate Development18
- ASP.NET Core Development24
- ASP.NET Development34
- ASP.NET Zero Development24
- C# Development23
- CakePHP Development9
- Codeigniter Development13
- CPANEL4
- Craft CMS Development9
- Cross Platform App Development15
- Custom CMS Development14
- Custom CRM Development5
- Custom Development32
- Dot Net Development30
- Drupal Development16
- eCommerce Website Development Services30
- Google Panda 4.21
- HTML 5 Development11
- Internet Marketing SEO6
- iOS App Development Services11
- iPad App Development56
- iPhone App Development55
- Joomla Development16
- Kentico Development20
- Laravel Development16
- Magento Development Services16
- Managed Services4
- Mobile Application Development Company34
- My SQL Server4
- News67
- Next JS Development1
- Offshore Development1
- Open Source Development65
- OpenCart Development3
- Pay Per Click21
- PHP Web Development65
- Power BI Development4
- Quality Assurance11
- React JS Development2
- React Native Web1
- Search Engine Optimization36
- SEO India24
- SharePoint Development22
- Shopify Development Agency8
- Social Media Optimization24
- Software Development India15
- Technology14
- UI/UX Design Company10
- Umbraco Development17
- Uncategorized1
- Wearable App Development8
- Web Application Development10
- Web Design and Development32
- WooCommerce Development15
- WordPress Customization27
- WordPress Development Company25
- Xamarin App Development Company11
- Zen Cart Development11

Advantages of ASP.NET Development
