Hello,
I have home project for learning asp.net . I have a web application which contain a login form.
So far application contain:
when user press login Button, at same time also a sql database table is updated. Table field is a Bit data type. So when user press login Button, column/row is updated to True. That would be look like that user is login at the moment.
But, when user close Browser, database column will still have value True.
So, I would like when Broswer is close, also to update sql database table column/row to False.
Any idea, if is it possible? Can be browser button handle behind asp.net code, like with Vb.net, or C#?
Hi,
first thanks for replies.
Second, I have been doing a little bit around Global.asax. This visual studio item looks fine me, but have a problem.
In Default.aspx site,code I have my login method with controls, one of them is UserNameTextBox, which found username from database, and past it to Session("Username") >> like lblUsername.text = Session("Username").
Now, in global I have import sqlclient namespace, and add a little code
But, now I have no idead how to get value from aspx page?!
Code is actual working, but only if I manualy add value for sql update query. I have been Google, but did not found any related issues. I have try to add same Session, but of course it is not working.
Now, I'm not sure if is it possible?!
Any, idea helpers?
<%@ Application Language="VB" %>
<%@ Import Namespace="System.data.sqlclient" %>
<script runat="server">
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "MyConnectionString"
con.Open()
cmd.Connection = con
cmd.CommandText = "UPDATE Login SET OnlineOffline = 'False' WHERE (UserName = '" & Session("UserName") & "')"
cmd.ExecuteNonQuery()
con.Close()
MsgBox("test close")
End Sub
dejanc
Member
32 Points
54 Posts
Is it possible to update sql server database when Internet Browser is close? When user will click...
Apr 26, 2012 07:14 PM|LINK
Hello,
I have home project for learning asp.net . I have a web application which contain a login form.
So far application contain:
when user press login Button, at same time also a sql database table is updated. Table field is a Bit data type. So when user press login Button, column/row is updated to True. That would be look like that user is login at the moment.
But, when user close Browser, database column will still have value True.
So, I would like when Broswer is close, also to update sql database table column/row to False.
Any idea, if is it possible? Can be browser button handle behind asp.net code, like with Vb.net, or C#?
Thank you in advance for help!
VS2010
adamturner34
Contributor
3824 Points
963 Posts
Re: Is it possible to update sql server database when Internet Browser is close? When user will c...
Apr 26, 2012 07:26 PM|LINK
You want to update that field when the session ends not when the browser closes, which can coincidentally be the same but not always.
You do that in the global.asax file on the Session_End event.
VS2010
deepak.vasud...
Contributor
2488 Points
622 Posts
Re: Is it possible to update sql server database when Internet Browser is close? When user will c...
Apr 26, 2012 10:37 PM|LINK
Session_onend may be a non-deterministic. Check out http://www.eggheadcafe.com/articles/20030416.asp
Deepak Vasudevan
http://www.lavanyadeepak.tk/
-------
sriramabi
Contributor
4351 Points
1277 Posts
Re: Is it possible to update sql server database when Internet Browser is close? When user will c...
Apr 26, 2012 10:41 PM|LINK
The only way is by using AJAX. Here's an example.
//////////////////////////////////////////////////////////////////////////
// Page1.aspx
//////////////////////////////////////////////////////////////////////////
<script type="text/javascript">
<!--
var g_databaseRecordKey = '<%= DatabaseRecordKey %>';
var g_isPostBack = false;
function callAjax(webUrl, queryString)
{
var xmlHttpObject = null;
try
{
// Firefox, Opera 8.0+, Safari...
xmlHttpObject = new XMLHttpRequest();
}
catch(ex)
{
// Internet Explorer...
try
{
xmlHttpObject = new ActiveXObject('Msxml2.XMLHTTP');
}
catch(ex)
{
xmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ( xmlHttpObject == null )
{
window.alert('AJAX is not available in this browser');
return;
}
xmlHttpObject.open("GET", webUrl + queryString, false);
xmlHttpObject.send();
var valueSent = xmlHttpObject.responseText;
return valueSent;
}
function doUpdate()
{
var webUrl = 'AjaxPage.aspx';
// Add any additional values needed for the update to the QueryString...
var queryString = '?CallRequest=UpdateDatabase&DbKey=' + g_databaseRecordKey;
var returnCode = callAjax(webUrl, queryString);
//alert('returnCode: ' + returnCode);
}
function windowOnUnload()
{
if ( g_isPostBack == true )
return; // Let the page unload
//alert('window.onunload fired');
doUpdate();
}
window.onunload = windowOnUnload;
// -->
</script>
//////////////////////////////////////////////////////////////////////////
// Page1.aspx.cs
//////////////////////////////////////////////////////////////////////////
protected string DatabaseRecordKey = "";
protected void Page_Load(object sender, EventArgs e)
{
// Set to the unique database record key for this user...
DatabaseRecordKey = "1234";
this.ClientScript.RegisterOnSubmitStatement(this.GetType(), "OnSubmitScript", "g_isPostBack = true;");
}
//////////////////////////////////////////////////////////////////////////
// AjaxPage.aspx.cs
//////////////////////////////////////////////////////////////////////////
protected string UpdateDatabase(string dbKey)
{
string returnValue = "OK";
// Update the database here setting the returnValue variable to "Failed" if not successful...
return returnValue;
}
private void Page_Load(object sender, System.EventArgs e)
{
string callRequest = (this.Request["CallRequest"] == null) ? string.Empty : this.Request["CallRequest"];
string returnValue = string.Empty;
if ( callRequest == "UpdateDatabase" )
{
string dbKey = (this.Request["DbKey"] == null) ? string.Empty : this.Request["DbKey"];
returnValue = UpdateDatabase(dbKey);
}
this.Response.ClearHeaders();
this.Response.Clear();
this.Response.Write(returnValue);
this.Response.End();
}
dejanc
Member
32 Points
54 Posts
Re: Is it possible to update sql server database when Internet Browser is close? When user will c...
Apr 29, 2012 02:53 PM|LINK
Hi,
first thanks for replies.
Second, I have been doing a little bit around Global.asax. This visual studio item looks fine me, but have a problem.
In Default.aspx site,code I have my login method with controls, one of them is UserNameTextBox, which found username from database, and past it to Session("Username") >> like lblUsername.text = Session("Username").
Now, in global I have import sqlclient namespace, and add a little code
But, now I have no idead how to get value from aspx page?!
Code is actual working, but only if I manualy add value for sql update query. I have been Google, but did not found any related issues. I have try to add same Session, but of course it is not working.
Now, I'm not sure if is it possible?!
Any, idea helpers?
<%@ Application Language="VB" %> <%@ Import Namespace="System.data.sqlclient" %> <script runat="server"> Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) Dim con As New SqlConnection Dim cmd As New SqlCommand con.ConnectionString = "MyConnectionString" con.Open() cmd.Connection = con cmd.CommandText = "UPDATE Login SET OnlineOffline = 'False' WHERE (UserName = '" & Session("UserName") & "')" cmd.ExecuteNonQuery() con.Close() MsgBox("test close") End SubThanks in advance for help.
dejanc
Member
32 Points
54 Posts
Re: Is it possible to update sql server database when Internet Browser is close? When user will c...
Apr 30, 2012 05:41 PM|LINK
Found a solution. I was only need to move code to Session_End :-).