2010-04-08 98 views
1

請幫忙,我已經在gridview中添加了更新/編輯命令按鈕,以便更新我的sql服務器數據庫中的數據,但我無法做到這一點。數據不在數據庫中更新。 onrowupdate ======代碼======================================== protected void gRowUpdate(object sender,GridViewUpdateEventArgs e) Books b = null; b = new Books(); DataTable dt = null; GridView g =(GridView)發件人; 嘗試 {dt = new DataTable(); b = new Books(); b.author = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Author」)); b.bookID = Convert.ToInt32(g.Rows [e.RowIndex] .FindControl(「BookID」)); b.title = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Title」)); b.price = Convert.ToDouble(g.Rows [e.RowIndex] .FindControl(「Price」)); // b.rec = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Date_of_reciept」)); b.ed = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.bill = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.cre_by = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.src = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.pages = Convert.ToInt32(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.pub = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.mod_by = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.remark = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); // b.year = Convert.ToString(g.Rows [e.RowIndex] .FindControl(「Edition」)); b.updatebook(b); g.EditIndex = -1; g.EditIndex = -1; dt = b.GetAllBooks(); g.DataSource = dt; g.DataBind(); (例外) } catch(例外ex) throw(ex);( ) } finally { b = null; }無法更新gridview

} ===================我的更新書存儲過程能夠通過exec在sqlserver mgmt studio中更新數據庫======= =================== 集ANSI_NULLS ON 設定QUOTED_IDENTIFIER ON 去

ALTER PROCEDURE [DBO]。[usp_updatebook] @bookid BIGINT, @author VARCHAR(50), @title VARCHAR(50), @price BIGINT, @src_equisition VARCHAR(50), @bill_no VARCHAR(50), @publisher VARCHAR( 50) @pages BIGINT, @remark VARCHAR(50), @edition VARCHAR(50), @created_by VARCHAR(50), @modified_by VARCHAR(50) /@date_of_reciept日期時間, @year_of_publication日期時間/ AS 聲明 @modified_on datetime

集@ modified_on = GETDATE()

UPDATE書

SET

作者= @作家, 標題= @標題, 價格= @價格,

src_equisition = @ src_equisition , bill_no = @ bill_no, publisher = @ publisher, /Date_of_reciept = @ date_of_reciept,/ 頁= @頁面, 此言= @此言一出, 版= @版, /Year_of_publication = @ year_of_publication,/

CREATED_BY = @ CREATED_BY, modified_on = @ modified_on, modified_by = @ modified_by

WHERE bookid = @ bookid ========================類庫函數更新============= =======

public void updatebook(Books b) 
    { 
     DataAccess dbAccess = null; 
     SqlCommand cmd = null; 
     try 
     { 
      dbAccess = new DataAccess(); 
      cmd = dbAccess.GetSQLCommand("usp_updatebook", CommandType.StoredProcedure); 
      cmd.Parameters.Add("@bookid", SqlDbType.VarChar, 50).Value = b.bookID; 
      cmd.Parameters.Add("@author", SqlDbType.VarChar, 50).Value = b.author; 
      cmd.Parameters.Add("@title", SqlDbType.VarChar, 50).Value = b.title; 
      cmd.Parameters.Add("@price", SqlDbType.Money).Value = b.price; 
      cmd.Parameters.Add("@publisher", SqlDbType.VarChar, 50).Value = b.pub; 
      // cmd.Parameters.Add("@year_of_publication", SqlDbType.DateTime).Value =Convert.ToDateTime(b.year); 
      cmd.Parameters.Add("@src_equisition", SqlDbType.VarChar, 50).Value = b.src; 
      cmd.Parameters.Add("@bill_no", SqlDbType.VarChar, 50).Value = b.bill; 
      cmd.Parameters.Add("@remark", SqlDbType.VarChar, 50).Value = b.remark; 
      cmd.Parameters.Add("@pages", SqlDbType.Int).Value = b.pages; 
      cmd.Parameters.Add("@edition", SqlDbType.VarChar, 50).Value = b.ed; 
      // cmd.Parameters.Add("@date_of_reciept", SqlDbType.DateTime).Value = Convert.ToDateTime(b.rec); 
      // cmd.Parameters.Add("@created_on", SqlDbType.DateTime).Value = Convert.ToDateTime(b.cre_on); 
      cmd.Parameters.Add("@created_by", SqlDbType.VarChar, 50).Value = b.cre_by; 
      //cmd.Parameters.Add("@modified_on", SqlDbType.DateTime).Value = Convert.ToDateTime(b.mod_on); 
      cmd.Parameters.Add("@modified_by", SqlDbType.VarChar, 50).Value = b.mod_by; 

      cmd.ExecuteNonQuery(); 

     } 
     catch (Exception ex) 
     { 
      throw (ex); 
     } 
     finally 
     { 
      if (cmd.Connection != null && cmd.Connection.State == ConnectionState.Open) 
       cmd.Connection.Close(); 
      dbAccess = null; 
      cmd = null; 
     } 

}

我還試圖通過以下保護無效gv1_updating(對象發件人,GridViewUpdateEventArgs E) { 的GridView G =(GridView的)發送器方式 做更新; abc a = new abc(); DataTable dt = new DataTable(); 嘗試 {

  a.cd_Id = Convert.ToInt32(g.DataKeys[e.RowIndex].Values[0].ToString()); 
      //TextBox b = (TextBox)g.Rows[e.RowIndex].Cells[0].FindControl("cd_id"); 
      TextBox c = (TextBox)g.Rows[e.RowIndex].Cells[2].FindControl("cd_name"); 
      TextBox d = (TextBox)g.Rows[e.RowIndex].Cells[3].FindControl("version"); 
      TextBox f = (TextBox)g.Rows[e.RowIndex].Cells[4].FindControl("company"); 
      TextBox h = (TextBox)g.Rows[e.RowIndex].Cells[6].FindControl("created_by"); 
      TextBox i = (TextBox)g.Rows[e.RowIndex].Cells[8].FindControl("modified_by"); 

      //a.cd_Id = Convert.ToInt32(b.Text); 
      a.cd_name = c.Text; 
      a.ver = d.Text; 
      a.comp = f.Text; 
      a.cre_by = h.Text; 
      a.mod_by = i.Text; 
      a.updateDigi(a); 
      g.EditIndex = -1; 
      dt = a.GetAllDigi(); 
      g.DataSource = dt; 
      g.DataBind(); 

     } 
     catch(Exception ex) 
     { 
      throw (ex); 
     } 
     finally 
     { 
      dt = null; 
      a = null; 
      g = null; 
     } 



    } 

===================但有指數的誤差超出範圍的異常=========

請不要回復,thanxs提前

回答

1

的IndexOutOfRangeException可能是由於此代碼:

 a.cd_Id = Convert.ToInt32(g.DataKeys[e.RowIndex].Values[0].ToString()); 
     TextBox c = (TextBox)g.Rows[e.RowIndex].Cells[2].FindControl("cd_name"); 
     TextBox d = (TextBox)g.Rows[e.RowIndex].Cells[3].FindControl("version"); 
     TextBox f = (TextBox)g.Rows[e.RowIndex].Cells[4].FindControl("company"); 
     TextBox h = (TextBox)g.Rows[e.RowIndex].Cells[6].FindControl("created_by"); 
     TextBox i = (TextBox)g.Rows[e.RowIndex].Cells[8].FindControl("modified_by"); 

更確切地說,其中索引發生的部分。你確定你有9個單元 (索引0到8)嗎?

+0

感謝您的回覆,我試圖通過更改索引,但仍然有相同error.Can你只需粘貼一個小例如gridview更新的代碼。在第一個方法上面沒有錯誤發生,但數據庫不更新。實際上沒有值從gridview單元格中讀取。急需它作爲最後一年項目的一部分。非常感謝你的回覆 – bhakti 2010-04-09 17:26:36