请帮助,我在gridview中添加了更新/编辑命令按钮,以便更新我的sql server数据库中的数据但是无法执行此操作。数据未在数据库中更新。 ====== onrowupdate的代码======================================== protected void gRowUpdate(object sender,GridViewUpdateEventArgs e) { 书籍b = null; b =新书(); DataTable dt = null; GridView g =(GridView)发送者; 尝试 {dt = new DataTable(); b =新书(); 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; dt = b.GetAllBooks(); g.DataSource = dt; g.DataBind(); } catch(Exception ex) { 扔(前); } 最后 { b = null; }
} ===================我的存储过程更新书能够通过exec在sqlserver mgmt studio中更新数据库=============== =========== 将ANSI_NULLS设置为ON 设置QUOTED_IDENTIFIER 去
ALTER PROCEDURE [dbo]。[usp_updatebook] @bookid bigint, @author varchar(50), @title varchar(50), @price bigint, @src_equisition varchar(50), @bill_no varchar(50), @publisherrs varchar(50), @pages bigint, @remark varchar(50), @edition varchar(50), @created_by varchar(50), @modified_by varchar(50) / @date_of_reciept datetime, @year_of_publication datetime / 如 宣布 @modified_on datetime
设置@ modified_on = getdate()
更新书籍
SET
作者= @作者, 标题= @标题, 价格= @价格,
src_equisition = @ src_equisition, bill_no = @ bill_no, 出版商= @出版商 / Date_of_reciept = @ date_of_reciept, / 页= @页面, 此话= @此言一出, 版= @版, / Year_of_publication = @ year_of_publication, /
CREATED_BY = @ CREATED_BY, modified_on = @ modified_on, modified_by = @ modified_by
在哪里bookid = @ bookid ========================用于更新的类库函数==================== < / p>
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;
}
我也试过按照以下方式进行更新 protected void gv1_updating(object sender,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;
}
}
===================但是索引错误超出范围异常=========
请事先回复,否则
答案 0 :(得分: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)?