2014-09-11 58 views
1

我已經嘗試使用DATATABLE來存儲整個應用程序的值。我沒有使用任何數據庫來永久存儲值。這是我可以在應用程序中使用的虛擬值。在C#中的數據表中動態添加多行

這個問題實際上是每當向我的DATATABLE中添加一個新的行值時,它就會覆蓋DATATABLE中的先前值而不是添加新行。

是否有機會動態地向DATATABLE添加多行,並且我可以在整個應用程序會話中使用它。

**我的註冊頁面**

public partial class _Default : System.Web.UI.Page 
{ 
    DataTable myDataTable; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     myDataTable = new DataTable(); 
     myDataTable.Columns.Add("username"); 
     myDataTable.Columns.Add("firstname"); 
     myDataTable.Columns.Add("lastname"); 
     myDataTable.Columns.Add("password"); 
     myDataTable.Columns.Add("conpassword"); 
     myDataTable.Columns.Add("email"); 

     myDataTable.Rows.Add("kavi1", "kavi", "rajan", "123456", "123456", "[email protected]"); 
     myDataTable.Rows.Add("sri1", "sri", "prem", "123456", "123456", "[email protected]"); 
     myDataTable.Rows.Add("mani1", "mani", "vanan", "123456", "123456", "[email protected]"); 
     HttpContext.Current.Session["name"] = myDataTable; 

     if (!IsPostBack) 
     { 


      if (Session["name"] != null) 
      { 

       myDataTable = Session["name"] as DataTable; 

      } 
     } 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 

     myDataTable.Rows.Add(new object []{ TextBox1.Text ,TextBox2.Text,TextBox3.Text , TextBox4.Text,TextBox5.Text ,TextBox6.Text }); 
     HttpContext.Current.Session["name"] = myDataTable; 

     if (Session["name"] != null) 
     { 
      Response.Write("success"); 

     } 

    } 

我的登錄頁面

public partial class login : System.Web.UI.Page 
{ 
    DataTable dtResult; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     dtResult = (DataTable)Session["name"]; 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 


     for (int i = 0; i < dtResult.Rows.Count; i++) 
     { 

      string un = dtResult.Rows[i].Field<string>("username"); 
      string fn = dtResult.Rows[i].Field<string>("password"); 


      if (TextBox1.Text == un && TextBox2.Text == fn) 
      { 
       //Response.Write("success"); 
       Response.Redirect("home.aspx"); 
      } 

     } 


    } 
+0

是它解決,或者您還有問題? – 2014-09-11 07:35:36

+0

沒有解決。 。 – 2014-09-12 09:39:34

回答

3

問題:

在每一個Postback要創建一個新datatable,並覆蓋到以前:

protected void Page_Load(object sender, EventArgs e) 
    { 
     myDataTable = new DataTable(); 
     //.... 
     HttpContext.Current.Session["name"] = myDataTable; //overriding to the previous 

所以,當你點擊按鈕添加新行,回發,並重新創建一個新的DataTable,然後添加一個新行到該表。

解決方案:

你只需要修改Page_Load事件處理程序:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (Session["name"] != null) //this is you have to do at every postback 
    { 
     myDataTable = Session["name"] as DataTable; 
    } 

    if (!IsPostBack) 
    { 
     //if it is not post back, create a new DataTable and add values to it 
     myDataTable = new DataTable(); 
     myDataTable.Columns.Add("username"); 
     myDataTable.Columns.Add("firstname"); 
     myDataTable.Columns.Add("lastname"); 
     myDataTable.Columns.Add("password"); 
     myDataTable.Columns.Add("conpassword"); 
     myDataTable.Columns.Add("email"); 

     myDataTable.Rows.Add("kavi1", "kavi", "rajan", "123456", "123456", "[email protected]"); 
     myDataTable.Rows.Add("sri1", "sri", "prem", "123456", "123456", "[email protected]"); 
     myDataTable.Rows.Add("mani1", "mani", "vanan", "123456", "123456", "[email protected]"); 
     HttpContext.Current.Session["name"] = myDataTable; 
    } 
} 
+0

你是對的@shaharyar ...現在它工作正常 – 2014-09-11 07:20:37