我尝试在gridview中更改数据的值时将数据更新到数据库 [英] i tried update the data to database while i change data's values in gridview

查看:105
本文介绍了我尝试在gridview中更改数据的值时将数据更新到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试将数据更新到数据库,我在GridView中更改了数据值。

我只是使用下面的代码进行更新,但在执行时,我收到了这个错误。



------------

错误:

指数超出范围。必须是非负数且小于集合的大小。

参数名称:index。



------- ---

代码:

Hi,
I tried updating the data to database and I changed data values in GridView.
I just use below code for updating, but at the time of execution, I got this error.

------------
error:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.

----------
code :

int curview = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[e.RowIndex];
      
string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
// int id=Convert.ToInt32(GridView1.DataKeys[curview].Value.ToString());
Response.Write(id);
TextBox tquantity = row.FindControl("txt_mtype") as TextBox;
string quan=tquantity.Text;
con.Open();
SqlCommand cmd3 = new SqlCommand("Update addtocart1 set quantity = @quantity where m_id = @AutoId", con);

cmd3.Parameters.AddWithValue("@quantity", quan.ToString());

cmd3.Parameters.AddWithValue("@AutoId", id);
cmd3.ExecuteNonQuery();

lblMessage.Text = "Record updated successfully !";
GridView1.EditIndex = -1;
this.BindData();
con.Close();

推荐答案

请稍微修改一下你的代码......

Please modify your code a little bit like below...
// Get the "DataKey" of the row being updated.
string id = GridView1.DataKeys[e.RowIndex].Value.ToString();

// Get the "index" of Row being edited.
int curview = GridView1.EditIndex;

// Get the row being edited by "curview".
GridViewRow row = GridView1.Rows[curview];

// Find the value of TextBox by "row".
TextBox tquantity = row.FindControl("txt_mtype") as TextBox;



此后继续编码。



试试看,让我吧知道。



谢谢......


After this continue your coding.

Try it and let me know.

Thanks...


这篇关于我尝试在gridview中更改数据的值时将数据更新到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆