为什么在后端C#上创建的对象变为空

时间:2017-02-28 00:29:37

标签: c# asp.net ajax

我使用asp.net和c#作为后端代码和html,javascript / jquery在前端

用户点击"详情"在网站上查看和编辑以前创建的请求。

jquery / ajax在后端调用方法以获取有关请求的信息。 创建一个保存此信息的对象newWaferRequest。该对象从ajax调用返回,并且该站点使用该信息填充表单。然后,用户可以编辑表单,向请求添加新程序,修改现有请求信息等......

我得到的问题是当用户选择"保存pgm"时,ajax调用后端savePgm方法,该方法应该更新先前创建的Object newWaferRequest,但我收到的是NullReferenceException异常。异常指向newWaferRequest对象为null。这在用户更新请求的不同时间发生。有时,用户可以更新程序并添加新程序,并从后端获得正确的响应。其他时候发生异常。

我不能完全掌握变量如何存储"使用网络时。我在这里阅读了多篇文章并阅读了有关Session和ViewState变量的内容。我试过在会话中保存对象而没有运气。

这只是显示我的c#代码的开头,我最初尝试使用静态变量。所有对象都在App_code cs文件中。

public partial class Nbump_Request : System.Web.UI.Page
{
//public static person currentPerson = new person();
//public static requestSearch searchRequest;
//public static waferRequest newWaferRequest;
//public static waferRequestObject newWaferRequestObject;

static person currentPerson = new person();
static requestSearch searchRequest;
static waferRequest newWaferRequest;
static waferRequestObject newWaferRequestObject;

这是现有请求的ajax方法,它创建对象

[WebMethod]
public static string queryRequestExisting(string request_pk)
{
    JavaScriptSerializer js = new JavaScriptSerializer();
    try
    {
        newWaferRequest = new waferRequest(request_pk, "EXISTINGREQUEST");
        newWaferRequestObject = newWaferRequest.CURRENTREQUEST;
        string json = js.Serialize(newWaferRequest);
        return json;
    }
    catch (Exception)
    {
        return null;
    } 
}

这是用户保存或更新程序信息时的ajax代码。我在代码中添加了检查并返回对象为null。

public static string savePgmData(Dictionary<string, string> pgm_requests)
{
    PgmRequestObject currentPgm;
    if (newWaferRequest != null)
    {
        try
        {
            //if (!Convert.ToBoolean(pgm_requests["pgmEdit"])) //not performed when request pgm being edited
             string rtnMessage;
            //create new pgm in waferRequest object
            if (pgm_requests["PGMRT_INDEX_PK"] == null)
            {
                int index = newWaferRequest.addNewPGMRequest() - 1;
                currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];
                string pgmPrimaryKey = currentPgm.PGMRT_INDEX_PK;
                rtnMessage = "{\"message\": \"Added and saved new pgm successfully\", \"primaryKey\":\"" + pgmPrimaryKey + "\"}";
            }
            //get pgm to update at index provided
            else if (pgm_requests["pgmRowIndex"] != "-1")
            {
                int index = Convert.ToInt32(pgm_requests["pgmRowIndex"]);
                currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];
                rtnMessage = "Edited and saved pgm successfully";
            }
            //save initial pgm
            else
            {
               currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[0];
               rtnMessage = "Initial pgm added successfully";
            }
 
 
            currentPgm.updatePGM(pgm_requests["pgmNameList"], pgm_requests["pgmNameText"]);
            currentPgm.updateStatus(pgm_requests["pgmStatus"]);
            currentPgm.updateWafers(pgm_requests["pgmWfrList"], pgm_requests["pgmWfrNum"]);
            currentPgm.updatePriority(pgm_requests["pgmTestOrder"]);
            currentPgm.updateFailBinRetest(pgm_requests["pgmFbr"], pgm_requests["pgmFbrBins"]);
            //currentPgm.CommitIntoDatabase(); // -- Called on individual requests updates before request saved
 
            return rtnMessage;
        }
        catch (Exception ex)
        {
            return "Failed to save program\n\n" + ex.Data + "\n" + ex.Message + "\n" + ex.StackTrace + "\n";
        }
    }
    else
    {
        return "newWaferRequest object is null\n";
    }
}

这是waferRequest类的简短片段。它位于app_code中,是一个cs文件。 pgmRequestList包含一个pgm对象数组。这是我在用户选择&#34;保存程序&#34;。

时更新的内容
public class waferRequest
{
    /*The class will hold request information, creation and delete.
     *
     */
    private waferRequestObject currentRequest;
    private ArrayList WaferRequestList = new ArrayList();
    private ArrayList PGMRequestList = new ArrayList();

以下是我为Session尝试的示例。我摆脱了静态变量并修改了方法

[WebMethod(EnableSession=true)]
public static string queryRequestExisting(string request_pk)
{
    waferRequest newWaferRequest;
    waferRequestObject newWaferRequestObject;

    JavaScriptSerializer js = new JavaScriptSerializer();
    try
    {
        newWaferRequest = new waferRequest(request_pk, "EXISTINGREQUEST");
        newWaferRequestObject = newWaferRequest.CURRENTREQUEST;

        HttpContext.Current.Session["newWaferRequest"] = newWaferRequest;
        HttpContext.Current.Session["newWaferRequestObject"] = newWaferRequestObject;

        string json = js.Serialize(newWaferRequest);
        return json;
    }
    catch (Exception)
    {
        return null;
    } 
}


//snippet of savePgmData with session 
public static string savePgmData(Dictionary<string, string> pgm_requests)
{
    waferRequest newWaferRequest = HttpContext.Current.Session["newWaferRequest"];

    PgmRequestObject currentPgm;
    if (newWaferRequest != null)
    {
        try
        {
            //if (!Convert.ToBoolean(pgm_requests["pgmEdit"])) //not performed when request pgm being edited
             string rtnMessage;
            //create new pgm in waferRequest object
            if (pgm_requests["PGMRT_INDEX_PK"] == null)
            {
                int index = newWaferRequest.addNewPGMRequest() - 1;
                currentPgm = (PgmRequestObject)newWaferRequest.PGMREQUESTLIST[index];

我受限于因为工作安全而允许显示的代码。如果需要更多信息,请告诉我。

此外,代码似乎在我的本地服务器上正常工作。问题是当我把它放在生产服务器或开发服务器上时 - 我无法访问这两个服务器进行更改。

0 个答案:

没有答案