2016-08-08 2 views
0

SQL Serverに接続しようとすると、pagemethodsによってセッションの値にアクセスする際に問題があります。pagemethodsを介してセッションにアクセスすると、SQL Serverに接続するときにwebServiceErrorがスローされます

ファイルをアップロードするためのuploadEngin.aspxページがあります。アップロード状態を監視するために、セッションに状態値(10%など)を保存します。 uploadEngin.aspxページでは、SQL Serverに接続してファイルの有効な拡張子を取得しています。私には基本的な問題があります。以下の例は、サンプルコードを示します。

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; 
} 

コードなしでif(!isPostBack)の内側にすべてが正常に動作し、私は」を参照してくださいこんにちは "メッセージ。私は(ただし、有効な拡張子を取得するために、SQL Serverに接続する)そのコードを使用すると、I

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

はどうすればこの問題を解決する必要がありますか?

+0

あなたのwebmethodに属性[WebMethod(EnableSession = true)]を追加してください。 – techspider

+0

コメントしてくれてありがとうございます。これを追加しますが、まだエラーが表示されます – siamak

+0

なぜ3つの 'g()'関数があるのか​​分かりません。 – techspider

答えて

0

ウェブメソッドのSession値にアクセスするには、WebMethod属性にEnableSessionプロパティを設定する必要があります。詳細については

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

MSDN linkを参照してください。

+0

回答ありがとうございます。 EnableSession = true)、それでも私はそのエラーが表示されます – siamak

関連する問題