2011-02-07 192 views
0
string Landcode = Session("landcode"); 

給出了一個錯誤信息:會話變量無法識別

錯誤2「會議」不會在目前情況下

我看到在智能感知字會存在名稱。會話變量在global.asax中聲明。

void Session_Start(object sender, EventArgs e) 
    { 
     // Code that runs when a new session is started 
     string landcode = Request["strLandCode"]; 
    } 

`

回答

1

你在哪裏試圖從?訪問Session對象?

用於獲取會話值將是(你也想檢查它的代碼是不是null調用的ToString()之前:

string landcode = Session["landcode"].ToString(); 

Request對象和Session對象是不是同一個對象太你將不得不做以下landcode添加到會話:

Session["landcode"] = strLandCode; 
+0

我有一個aspx頁面(default.aspx)與JavaScript。在javascript部分有一個ajax調用,將國家代碼發送到global.asax($ .post(「Global.asax」,{strLandCode:LandCode});)那就是我寫的字符串landcode = Request [「strlandcode」] – Thomas 2011-02-07 13:35:44

-1

這是如何在ASP.NET中使用會話和C#

//This how to add value in session "ID" Is the name of the session and "1" is the value 
Session.Add("ID", 1); 
//How to retrieve the value which is in the Session 
int ID = Convert.ToInt16(Session["ID"]); 
//write session Value 
Response.Write(ID.ToString()); 

請試試吧,告訴我們結果

+1

downvoting當! – 2011-02-07 13:04:18

1

使用HttpContext.Current.Session["landcode"]

會議是一種字典,所以你用索引它而不是使用一個方法調用,即()

而在C#中也有投每個對象因此讓字符串對象時(string)之前,獲得一個int時(int)等先...

+0

我想這並請發表評論,它似乎是字符串Landcode(所以第一個)沒有按」沒有充滿會話變量,我改變了第二個代碼位到字符串landcode =「BE」。但字符串Landcode仍然爲空。 – Thomas 2011-02-07 13:19:58