Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want that when I close the browser a popup should confirm "Are you sure you want to exit?" If yes, the browser should close and current date should be saved into the database.
I have tried a code as
protected void Page_UnLoad(object sender, EventArgs e)
    {   
        DateTime d = System.DateTime.Now;
        string cn = "Data Source=hcl;Initial Catalog=mydata;Integrated Security=True";
        SqlConnection con = new SqlConnection(cn);
        SqlCommand cmd = new SqlCommand("insert into mydate values(@d)", con);
        con.Open();
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add(new SqlParameter("@d", d));
        // cmd.Parameters.Add(new SqlParameter("@b", b));
        cmd.ExecuteNonQuery();
        con.Close();

    }


but it was not succesful. Also before I close the browser the data is saved in the database. Also I don't know how to call alert function as "Are you sure you want to exit." Please help.

[Wrapped code sample in pre tags and edited to clean up txtspk]
Posted
Updated 20-Sep-10 8:19am
v2
Comments
software_Engi08 20-Sep-10 14:56pm    
what is that [Wrapped code sample in pre tags and edited to clean up txtspk] i still does not got the answer plz
help how to show popup and saving data to database when we close the browser.
Sandeep Mewara 20-Sep-10 15:18pm    
[Wrapped code sample in pre tags and edited to clean up txtspk] :
That means your code part was formatted and made readable using PRE tags. Your 'text speak' words like 'plz' were removed. Avoid using text speak.
software_Engi08 21-Sep-10 14:20pm    
now what i have done is that on i have called this function and written code as window.close(); .and in global.asax form in application_end method i have call a insert function which i have written in class file.this is doing but only one time the date is being saved into the database.but second when i run the application the date is not being saved in database. then again i have to delete the global.asax form and remake the file then the date is being saved successfully.but i dont want to repeat this step please help.give some proper code

1 solution

Your problem is that the server never knows when the browser is closed or when the user navigates away from your application.

The BROWSER knows when this happens though, so you'll have to implement this functionality in JavaScript code in the web page that your ASP.NET code sends to the client.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900