0

セッションに何らかの値を割り当てている状況があります。そのコードが繰り返し呼び出されている状況があります。時々、このコードはオブジェクト null のエラーをスローします。値を割り当てている間になぜこれが起こっているのかわかりません。私のコードは

  if (HttpContext.Current.Cache["Cache"] == null)
            {
                CreateChanhe();
                DataTable dtcache= HttpContext.Current.Cache["HNS Connection"] as DataTable; //CreateConnectString(CCMMUtility.Encryptdata(txtPin.Text));
                string sqlFilter = "Sp = '" + classses.DecryptString(HttpContext.Current.Request.Cookies["Cookies"].Values["sp"].ToString(), classses.SP) + "'";
                DataRow[] dr = dtcache.Select(sqlFilter);
                if (dr.Length > 0)
                {
                    String[] Con = new String[2];
                    Con[0] = dr[0][0].ToString();
                    Con[1] = dr[0][1].ToString();
                    sp= Con[1]; 
                    HttpContext.Current.Session["Name"] = dr[0][2].ToString();
                }
            }

デバッグ中に「ウォッチの追加」を実行しようとすると、「値」には値がありますが、HttpContext.Current.Session["Name"]は null です。なぜこれが起こっているのか教えてください。

実際にキャッシュを作成し、そのキャッシュからセッションを埋めます。これは私の要件に基づいています。

4

2 に答える 2

0

HttpContext.Current は null ですか? その場合は、次のように null チェックをラップします。

if (HttpContext.Current != null)
{
 if (HttpContext.Current.Cache["Cache"] == null)
            {
                CreateChanhe();
                DataTable dtcache= HttpContext.Current.Cache["HNS Connection"] as                        DataTable; //CreateConnectString(CCMMUtility.Encryptdata(txtPin.Text));
                string sqlFilter = "Sp = '" + classses.DecryptString(HttpContext.Current.Request.Cookies["Cookies"].Values["sp"].ToString(), classses.SP) + "'";
                DataRow[] dr = dtcache.Select(sqlFilter);
                if (dr.Length > 0)
                {
                    String[] Con = new String[2];
                    Con[0] = dr[0][0].ToString();
                    Con[1] = dr[0][1].ToString();
                    sp= Con[1]; 
                    HttpContext.Current.Session["Name"] = dr[0][2].ToString();
                }
            }
}
于 2012-07-05T06:03:02.157 に答える
0

それdr[0][2]はnullだと思うので、それを呼び出すと.ToString()NREが得られます。

于 2012-07-05T06:01:48.707 に答える