首页 文章

无法更新gridview

提问于
浏览
1

请帮助,我已经在gridview中添加了更新/编辑命令按钮,以便更新我的sql server数据库中的数据,但我无法做到 . 数据未在数据库中更新 . ====== onrowupdate的代码======================================== protected void gRowUpdate(object sender,GridViewUpdateEventArgs e){Books 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){throw(ex); } finally

} ===================我的存储过程更新书能够通过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,@ redmark varchar(50),@ edit_var varchar(50),@ modified_by varchar(50)/ @ date_of_reciept datetime,@ year_of_publication datetime / AS声明@modified_on datetime

设置@ modified_on = getdate()

更新书籍

author = @ author,title = @ title,price = @ price,

src_equisition = @ src_equisition,bill_no = @ bill_no,publisher = @ publisher,/ Date_of_reciept = @ date_of_reciept,/ pages = @ pages,remark = @ remark,edition = @ edition,/ Year_of_publication = @ year_of_publication,/

created_by = @ created_by,modified_on = @ modified_on,modified_by = @ modified_by

在哪里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;
        }

}

我还尝试通过以下方式进行更新:protected void gv1_updating(object sender,GridViewUpdateEventArgs e){GridView g =(GridView)sender; 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;
        }



    }

===================但是索引错误超出范围异常=========

请事先回复,谢谢

1 回答

  • 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)吗?

相关问题