unable to update gridview

Posted by bhakti on Stack Overflow See other posts from Stack Overflow or by bhakti
Published on 2010-04-08T16:02:46Z Indexed on 2010/04/08 16:23 UTC
Read the original article Hit count: 362

Filed under:
|

Please help ,i have added update/edit command button in gridview so to update data in my sql server database but am unable to do it. Data is not updated in database . ======code for onrowupdate======================================== protected void gRowUpdate(object sender, GridViewUpdateEventArgs e) { Books b = null; b = new Books(); DataTable dt=null; GridView g = (GridView)sender; try { 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; dt = b.GetAllBooks(); g.DataSource = dt; g.DataBind(); } catch (Exception ex) { throw (ex); } finally { b = null; }

} ===================My stored procedure for update book able to update database by exec in sqlserver mgmt studio========================== set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go

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 datetime, @year_of_publication datetime*/ AS declare @modified_on datetime

set @modified_on=getdate()

UPDATE books

SET

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

WHERE bookid=@bookid ========================class library function for update====================

    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;
        }

}

I have also tried to do update by following way protected void gv1_updating(object sender, GridViewUpdateEventArgs e) { GridView g = (GridView)sender; abc a = new abc(); DataTable dt = new DataTable(); try {

            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;
        }



    }

=================== but have error of Index out of range exception=========

please do reply,thanxs in advance

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#