2013-06-24 112 views
0

我知道你們大多數人會說谷歌這一點。但我已經這樣做了,仍然無法弄清楚爲什麼按鈕事件點擊數據仍然不會被插入到數據表中。 忽略數據本身,它是隨機的。從ASP.Net格式插入數據到數據表

任何幫助將不勝感激。

 public void btnAddRow_Click(object sender, EventArgs e) 
    { 
     DataSet ds = (DataSet)(Session["DS"]); 
     TestGrid.DataSource = ds.Tables[0]; 
     ds.Tables[0].Rows.Add(); 

     DataTable dt = (DataTable)(Session["DT"]); 
     DataRow dr = null; 
     dr = dt.NewRow(); 



     //add values to each rows 
     dr["Title"] = txtTitle.Text; 
     dr["Year"] = txtYear.Text; 
     dr["Type"] = txtType.Text; 
     dr["Lead Actor"] = txtLeadActor.Text; 
     dr["Producer"] = txtProducer.Text; 
     dr["Director"] = txtDirector.Text; 

     dt.Rows.Add(dr); 
     TestGrid.DataSource = dt; 
     TestGrid.DataBind(); 

    } 

    private void SetInitialRow() 
    { 

     DataTable dt = new DataTable(); 
     DataRow dr = null; 
     Session["DT"] = dt; 

     //define the columns 
     dt.Columns.Add(new DataColumn("Title", typeof(string))); 
     dt.Columns.Add(new DataColumn("Year", typeof(string))); 
     dt.Columns.Add(new DataColumn("Type", typeof(string))); 
     dt.Columns.Add(new DataColumn("Lead Actor", typeof(string))); 
     dt.Columns.Add(new DataColumn("Producer", typeof(string))); 
     dt.Columns.Add(new DataColumn("Director", typeof(string))); 


     //create new row 
     dr = dt.NewRow(); 


     //add values to each rows 
     dr["Title"] = "Blade"; 
     dr["Year"] = "1989"; 
     dr["Type"] = "Action"; 
     dr["Lead Actor"] = "Wesley Snipes"; 
     dr["Producer"] = "Guy"; 
     dr["Director"] = "Dude"; 


     //add the row to DataTable 
     dt.Rows.Add(dr); 
     TestGrid.DataSource = dt; 
     TestGrid.DataBind(); 


    } 

回答

1

該代碼看起來正確。

DataTable _table = new DataTable() 
DataRow _row = _table.NewRow() 
_table.Rows.Add(_row) 

我認爲這可能是您的回傳。在Rows.Add(_row)之後放置一個斷點,並確保數據表具有數據。我認爲數據在回發/頁面加載時被刪除。

+0

我想通了。非常簡單的錯誤。代碼是正確的,我從來沒有在按鈕事件本身調用它。 轉折點是個好主意。 對不起,浪費時間>。> – messif