ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Delete data from database on Browser (Tab) Close using JavaScript and C# in ASP.Net<p>Hi <a class="username" rel="aspsun11"> aspsun11</a>,</p> <p>To delete particular data on browser close follow the below steps.</p> <p>1. After successfull Login set Session variable to identify the user. In the example i have used IsValidLogin and UserId.</p> <p>2. Then create a JavaScript function to make AjaxCall to a page for delete the record on browser close.</p> <p>3. Then call the JavaScript function and pass the required parameter.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">JavaScript</span></strong></p> <pre class="brush: js">function AjaxCall(userId) { var webUrl = &#39;Delete_Record.aspx?UserID=&#39; + userId; var xmlHttpObject = null; try { // Firefox, Opera 8.0+, Safari... xmlHttpObject = new XMLHttpRequest(); } catch (ex) { // Internet Explorer... try { xmlHttpObject = new ActiveXObject(&#39;Msxml2.XMLHTTP&#39;); } catch (ex) { xmlHttpObject = new ActiveXObject(&#39;Microsoft.XMLHTTP&#39;); } } if (xmlHttpObject == null) { window.alert(&#39;AJAX is not available in this browser&#39;); return; } xmlHttpObject.open(&#34;GET&#34;, webUrl, false); xmlHttpObject.send(); }</pre> <p><strong>Login.aspx</strong></p> <pre class="brush: html">&lt;asp:Button Text=&#34;Login&#34; runat=&#34;server&#34; OnClick=&#34;Validate&#34; /&gt;</pre> <p>&nbsp;<strong>Login.aspx.cs</strong></p> <pre class="brush: csharp">protected void Validate(object sender, EventArgs e) { // If Valid Login. if (1 == 1) { Session[&#34;IsValidLogin&#34;] = true; Session[&#34;UserId&#34;] = &#34;12345&#34;; Response.Redirect(&#34;~/Default.aspx&#34;); } }</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script src=&#34;BrowserCloseCall.js&#34; type=&#34;text/javascript&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; window.onunload = function () { AjaxCall(&#39;&lt;%= Session[&#34;UserID&#34;] %&gt;&#39;); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong>Namespaces</strong></p> <pre class="brush: csharp">using System.Configuration; using System.Data; using System.Data.SqlClient;</pre> <p><strong>Delete_Record.aspx.cs</strong></p> <pre class="brush: csharp">private void Page_Load(object sender, System.EventArgs e) { // If user logged in. if ((bool)Session[&#34;IsValidLogin&#34;]) { // If UserId is exists. if (!string.IsNullOrEmpty(Request.QueryString[&#34;UserID&#34;])) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(&#34;DELETE FROM Table WHERE UId = @UserId&#34;)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue(&#34;@UserId&#34;, Request.QueryString[&#34;UserID&#34;]); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } } }</pre> <p>After login IsValidLogin and UserId is set and redirect to Default page.</p> <p>When user close browser AjaxCall is executed inside window.onunload function and Session["UserID"] is passed and Delete_Record page called and record deleted from table.</p>https://www.aspforums.net:443/Threads/607085/Delete-data-from-database-on-Browser-Tab-Close-using-JavaScript-and-C-in-ASPNet/https://www.aspforums.net:443/Threads/607085/Delete-data-from-database-on-Browser-Tab-Close-using-JavaScript-and-C-in-ASPNet/Thu, 22 Aug 2019 04:59:01 GMT