Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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

C#
[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
JavaScript
//------------------------------
        // Save uploaded file data in database
        //------------------------------
        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 {
                        //DisplayMessage("Datatransfer process 100% completed", "Success", false);
                        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 {
                        //I am not getting output here...
                        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.
Posted

Try using response instead of result. I'm surprised JavaScript isn't throwing an error.

JavaScript
DisplayMessage(result.Message, "Information", false);


At that point in the code, that function shouldn't know what result is. Because you specify response as the variable:

JavaScript
function (response)


I could easily be wrong in this, but I think that might be it!

Good Luck!
 
Share this answer
 
You have to call
JavaScript
displayRecordStatusOn();

method on success event of
JavaScript
SaveFileData
Method. Then you will get HttpContext.Current.Cache["RecordStatus"] value.
 
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