How to update the login time and log out time in database using Login button and Logout button.

Posted by Valley under ASP.NET AJAX on 5/9/2013 | Points: 10 | Views : 2641 | Status : [Member] | Replies : 2
How to update the login time and log out time in database using Login button and Logout button.

I would like to call a store proc to update the date and time.

Please explain the aspx.cs part also.




Responses

Posted by: Learningtorise on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
Courstesy: http://www.mikesdotnetting.com/Article/104/Many-ways-to-communicate-with-your-database-using-jQuery-AJAX-and-ASP.NET

Page Methods
A Page Method is a static method that belongs to its Page class. As such, it can be placed in a <script runat="server"> block, or in code-behind. Since I am already using code-behind to populate the DropDownList on PageLoad() in Customer.aspx, I'll stick with the code-behind approach. ASP.NET 3.5 methods will always serialize and return a JSON object wrapped inside another one: d, if the request contentType is set to application/json.
To add the method to the code behind, two additional references are needed:

using System.Text;

using System.Web.Services;

These will allow me to use a StringBuilder object to build the return value, and to adorn the Page Method with the [WebMethod] attribute. The full method is as follows:

[WebMethod]
public static string FetchCustomer(string CustomerID)
{
string response = "<p>No customer selected</p>";
string connect = "Server=MyServer;Database=Northwind;Trusted_Connection=True";
string query = "SELECT CompanyName, Address, City, Region, PostalCode," +
"Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID";
if (CustomerID != null && CustomerID.Length == 5)
{
StringBuilder sb = new StringBuilder();
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("CustomerID", CustomerID);
conn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
sb.Append("<p>");
sb.Append("<strong>" + rdr["CompanyName"].ToString() + "</strong><br />");
sb.Append(rdr["Address"].ToString() + "<br />");
sb.Append(rdr["City"].ToString() + "<br />");
sb.Append(rdr["Region"].ToString() + "<br />");
sb.Append(rdr["PostalCode"].ToString() + "<br />");
sb.Append(rdr["Country"].ToString() + "<br />");
sb.Append("Phone: " + rdr["Phone"].ToString() + "<br />");
sb.Append("Fax: " + rdr["Fax"].ToString() + "</p>");
response = sb.ToString();
}
}
}
}
}
return response;
}


It's more or less identical to the ASPX version. The jQuery code is too:

<script type="text/javascript" src="script/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
$('#Customers').change(function() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: "{ CustomerID: '" + $('#Customers').val() + "'}",
url: "Customer.aspx/FetchCustomer",
dataType: "json",
success: function(data) {
$("#CustomerDetails").html(data.d);
}
});
});
});
</script>



http://hashtagakash.wordpress.com/

Valley, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Valley on: 5/10/2013 [Member] Starter | Points: 25

Up
0
Down
I am so sorry to say this but your answer was not related to my question.

Valley, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response