2012-02-13 126 views
0

我正在使用MS Access在ASP.net(C#)中做一個小應用程序。我有一個Gridview,我有一個「更新」命令。在運行期間,我在RowUpdating()方法中放置了斷點。一切正常,沒有錯誤,參數在後面的代碼中得到更新。但在輸出中,gridview沒有更新,也沒有在數據庫中。Gridview更新錯誤

這裏是代碼:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{  
    string ID = GridView1.Rows[e.RowIndex].Cells[1].Text; 
    string CName= ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text; 
    string AName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; 
    string APhone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; 
    string AEmail = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text; 
    string Note = ((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text; 

    UpdateRecord(ID, CName, AName, APhone, AEmail, Note); // GridView1.EditIndex = -1; BindGridView(); 
} 

UpdateRecord()方法:

更新方法效果很好,使用斷點在運行過程中的所有參數獲取更新的值,但它是未在網格視圖中顯示,未在數據庫中更新

private void UpdateOrAddNewRecord(string ID, string CName, string AName, string APhone, string AEmail, string Note)  
{ 
    OleDbConnection connection = null; 
    string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\arjun.giridhar\Desktop\db1.mdb;Persist Security Info=False"; 

    string sqlStatement = "UPDATE Carriers" + " SET CarrierName = @CarrierName, AccountRepName = @AccountRepName, AccountRepContactPhone = @AccountRepContactPhone, AccountRepEmail= @AccountRepEmail, [email protected]" + " WHERE CarrierID = @CarrierID"; 

    try 
    { 
     connection = new OleDbConnection(conStr); 

     connection.Open(); 
     OleDbCommand cmd = new OleDbCommand(sqlStatement, connection); 
     cmd.Parameters.AddWithValue("@CarrierID", ID); 
     cmd.Parameters.AddWithValue("@CarrierName", CName); 
     cmd.Parameters.AddWithValue("@AccountRepName", AName); 
     cmd.Parameters.AddWithValue("@AccountRepContactPhone", APhone); 
     cmd.Parameters.AddWithValue("@AccountRepEmail", AEmail); 
     cmd.Parameters.AddWithValue("@Notes", Note); 
     cmd.CommandType = CommandType.Text; 
     cmd.ExecuteNonQuery(); 
    } 
    catch (Exception ex) 
    { 
     string msg = "Update Error:"; 
     msg += ex.Message; 
     throw new Exception(msg); 

    } 
    finally 
    { 
     connection.Close(); 
    } 
} 

BindGridView()方法:

private void BindGridView()  
{ 
    DataTable dt = new DataTable(); 
    OleDbConnection connection = new OleDbConnection(conStr); 
    try 
    { 
     connection.Open(); 
     string sqlStatement = "SELECT * FROM Carriers"; 
     OleDbCommand cmd = new OleDbCommand(sqlStatement, connection); 
     OleDbDataAdapter sqlDa = new OleDbDataAdapter(cmd); 
     sqlDa.Fill(dt); 
     if (dt.Rows.Count > 0) 
     { 
      GridView1.DataSource = dt; 
     GridView1.DataSourceID = string.Empty; 
      GridView1.DataBind(); 
     } 
    } 
    catch (System.Data.SqlClient.SqlException ex) 
    { 
     string msg = "Fetch Error:"; 
     msg += ex.Message; 
     throw new Exception(msg); 
    } 
    finally 
    { 
     connection.Close(); 
    } 

} 

請給我一個解決方案。

問候, 阿瓊

+3

請附上您的代碼示例 – sharad 2012-02-13 12:59:45

+1

附加代碼將爲您提供更多幫助順便說一句,您有更新後調用Gridview(rebind Gridview)的DataBind()方法。 – Devjosh 2012-02-13 13:02:07

+0

protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e) {string ID = GridView1.Rows [e.RowIndex] .Cells [1] .Text;string CName =((TextBox)GridView1.Rows [e.RowIndex] .Cells [2] .Controls [0])。字符串AName =((TextBox)GridView1.Rows [e.RowIndex] .Cells [3] .Controls [0])。字符串APhone =((TextBox)GridView1.Rows [e.RowIndex] .Cells [4] .Controls [0])。 string AEmail =((TextBox)GridView1.Rows [e.RowIndex] .Cells [5] .Controls [0])。Text; string Note =((TextBox)GridView1.Rows [e.RowIndex] .Cells [6] .Controls [0])。Text; – 2012-02-13 13:13:37

回答

1

我發現另一篇文章在這裏:Gridview not updating value它說,這是由Page_Load結合網格和不檢查回發引起的。我將Page_load更改爲:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      BindGridView(); 
     } 
    } 

工作正常。

+0

對不起,代碼的格式。我確實試圖弄清楚,但沒有成功。 – 2013-03-07 15:56:50