2010-04-08 96 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