爲什麼下面的代碼產生錯誤「對象引用未設置爲對象的實例即使我在按鈕點擊時使用set session方法事件?無法投射會話變量包含要列出的對象列表
public partial class MergePopUp : System.Web.UI.Page
{
List<MergeAccounts> Mlist = new List<MergeAccounts>();
protected void Page_Load(object sender, EventArgs e)
{
}
private void SetSession()
{
if (Session["Account"] == null)
{
Session["Account"] = Mlist;
}
}
protected void AddToMergeList_Click(object sender, EventArgs e)
{
MergeAccounts obj = new MergeAccounts();
obj.AccountNumber = Convert.ToInt32(AccountNumber.Text);
obj.PinNumber = Convert.ToInt32(PinNumber.Text);
int temp = obj.IsAccNoValid(obj.AccountNumber, obj.PinNumber);
if (temp == 0)
{
FoundOrNot.Visible = true;
FoundOrNot.Text = "Enter a valid Pin Number against this Account number";
}
else
{
DataSet MyDataSet = obj.ShowDetails(obj.AccountNumber, obj.PinNumber);
foreach (DataRow myRow in MyDataSet.Tables[0].Rows)
{
AccountTitle.Text = myRow["Title"].ToString();
Balance.Text = myRow["Balance"].ToString();
CreationDate.Text = myRow["CreationDate"].ToString();
}
obj.AccountTitle = AccountTitle.Text;
obj.Balance = Convert.ToDouble(Balance.Text);
obj.CreationDate = Convert.ToDateTime(CreationDate.Text);
Mlist.Add(obj);
Session["Account"] = Mlist;
SetSession();
}
}
protected void AddNext_Click(object sender, EventArgs e)
{
foreach (TextBox i in this.Page.Form.Controls.OfType<TextBox>().ToList())
{
i.Text = null;
}
FoundOrNot.Visible = false;
}
protected void CheckList_Click(object sender, EventArgs e)
{
SetSession();
Response.Redirect("Merge.spx");
}
}
你能告訴我們您最初充滿你的會話[「帳戶」]?因爲現在你正試圖將Session [「account」]中的任何內容傳遞給Mlit。這可能不起作用,因爲你沒有正確填寫會話。 – Thousand