0

I have a problem with accessing values in the session through pagemethods when I try connecting to SQL Server.

I have uploadEngin.aspx page for uploading files and for monitoring the upload state I store the state value (like 10%) in session. In the uploadEngin.aspx page I am connecting to SQL Server to get the valid extension for files. I have a basic problem. Example below show a sample code:

uploadEngin:

protected void Page_Load(object sender, EventArgs e)
{
    this.Session["s"] = "hello";

    if (!IsPostBack)
    {
        admin.app.core.attachment.AttachmentType att = new app.core.attachment.AttachmentType();
        att.GetExtentionAndMainPath("Image");
    }
}

[System.Web.Services.WebMethod(EnableSession = true)]
public static String g()
{
    return HttpContext.Current.Session["s"].ToString();
}

Javascript:

(function () {
    window.onload = function () {
        PageMethods.g(function (r) { alert(r); }, function (r) {
            console.log(r);
        });
    }
})();

GetExtentionAndMainPath:

public String[] GetExtentionAndMainPath(String name)
{
    String[] ext =new String[2];
    String x = name;
    UInt64 id = FindIdByName(x);

    DataTable dt = new DataAccess().ExecuteSelect("Select_ano_attachmentType", CommandType.StoredProcedure, new DataParam("@id", id, SqlDbType.BigInt));

    if (dt.Rows.Count > 0)
    {
        ext[0] = dt.Rows[0]["attachmentType_validExtention"].ToString();
        ext[1]= dt.Rows[0]["attachmentType_mainPath"].ToString();
    }

    return ext;
}

Without code inside if(!isPostBack) everything works fine and I see the "hello" message. When I use that code however (connecting to SQL Server to get the valid extension), I get

WebServiceError: Object reference not set to an instance of an object

How should I solve this problem?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
siamak
  • 1
  • 3

1 Answers1

0

You need to set EnableSession property on your WebMethod attribute in order to access Session values in your web method.

[WebMethod(EnableSession = true)]
public static String g()
{
    return HttpContext.Current.Session["s"].ToString();
}

For more details, please refer the MSDN link.

techspider
  • 3,370
  • 13
  • 37
  • 61