Hi
I have create two functions in my WebService and i am calling those methods from aspx page using Jquery.
My requirement are.
When i click on upload button it will start uploading data from excel (BulkCopy), and it will contains thousands of data in excel file.
While transfering data from Excel file to Database i would like to display progress on client browser like. 1500 of 21500 copied successfully! at 8/25/2011 12:09 PM
Code snippet:
Web Service
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string SaveFileData(string FileName, string ClientID, string FileAutoID)
{
try
{
DataTable dtExcelData = ExcelDataLoader(FileName, ClientID, "");
long TotalRecords = dtExcelData.Rows.Count;
long CopiedRecords = 0;
using (SqlConnection cn = new SqlConnection(GetConnectionString()))
{
SqlCommand cmdCopiedRecords = new SqlCommand("SELECT COUNT(*) FROM " + string.Format(DataBankTableFormat, FileAutoID) + ";", cn);
cn.Open();
using (SqlBulkCopy copy = new SqlBulkCopy(cn))
{
copy.BatchSize = BatchSize;
for (int colIndex = 0; colIndex < dtExcelData.Columns.Count; colIndex++)
{
copy.ColumnMappings.Add(colIndex, colIndex);
}
copy.DestinationTableName = string.Format(DataBankTableFormat, FileAutoID);
copy.WriteToServer(dtExcelData);
CopiedRecords = System.Convert.ToInt32(cmdCopiedRecords.ExecuteScalar());
HttpContext.Current.Cache["RecordStatus"] = string.Format("{0} of {1} copied successfully! at {2}", CopiedRecords, TotalRecords, DateTime.Now.ToString());
}
cn.Close();
}
Response = GetResponse(true, Convert.ToString(HttpContext.Current.Cache["RecordStatus"]));
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetRecordsInsertStatus(string FileAutoID)
{
try
{
Response = GetResponse(true, Convert.ToString(HttpContext.Current.Cache["RecordStatus"]));
}
catch (Exception ex)
{
Response = GetResponse(false, ex.Message);
}
return Response;
}
Code snippet:
.aspx Jquery Ajax
function SaveFileData() {
DisplayMessage("Uploading bulk data from file to database, this will take time please wait...", "Loading", false);
displayRecordStatusOn();
$.ajax({
type: "POST",
url: "SaveData.asmx/SaveFileData",
data: "{'FileName':'" + savedFileName + "', 'ClientID':'" + GetSelectedClient() + "','FileAutoID':'" + savedFileAutoID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
DisplayMessage("Error : " + result.Message, "Failed", false);
return;
}
else {
displayRecordStatusOff();
DisplayMessage(result.Message, "Success", false);
alert("100% Done!!");
}
}
});
}
function displayRecordStatusOn() {
$.ajax({
type: "POST",
url: "SaveData.asmx/GetRecordsInsertStatus",
data: "{'FileAutoID':'" + savedFileAutoID + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var result = jQuery.parseJSON(response.d);
if (result.Success == "False") {
DisplayMessage(result.Message, "Error", false);
}
else {
DisplayMessage(result.Message, "Information", false);
}
}
});
recordStatusTimer = setTimeout("displayRecordStatusOn()", 1500);
}
Problem:-
I am trying to access value of HttpContext.Current.Cache["RecordStatus"] in GetRecordsInsertStatus using ajax but it will always return empty.