如何使用C#更新数据库中gridview选中的复选框行 [英] How to update gridview selected checkbox row in database using C#

查看:139
本文介绍了如何使用C#更新数据库中gridview选中的复选框行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在gridview记录如下



Selectdata ID数量价格状态



复选框1245 2 50新

复选框1245 1 25新

复选框4568 1 10新

复选框2345 1 25新

复选框4568 1 10新



从上面的gridview我正在更新我从gridview中选择前两行并在数据库中更新



String strConnString = ConfigurationManager.ConnectionStrings [ConnectionStrings]。ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

con.Open();

foreach(grdRpt.Rows中的GridViewRow行)

{

CheckBox cb =(CheckBox)row.FindControl(chkselecdata);

if(cb.Checked == true)

{

int key = Convert.ToInt32(grdRpt.DataKeys [row.RowIndex] .Value);

SqlCommand cmd = new SqlCommand(UPDATE [transact]。[transaction_item] SET Status ='Ready'where transactee_id =+ key.ToString(),con);

cmd.ExecuteNonQuery();

}

}

con.Close();



i我正在使用c#



更新gridview所选行到数据库运行模式我在gridview中为id 1245选择第一行,但在数据库1245中为数据库中更新的两行选择。



i我没有选择第二行在gridview中

在运行模式下我只选择gridview中的第一行来更新数据库。但是在数据库中有两行1245正在更新。



上面代码中的错误是什么。





数据库输出我得到如下



ID数量价格状态



1245 2 50准备好

1245 1 25准备好



i我只选择gridview中的第一行。但是在数据库中有两行更新了那个选择的ID 1245



上面的代码有什么错误。



我的尝试:



在gridview记录中如下



选择数据ID数量价格状态



复选框1245 2 50新

复选框1245 1 25新

复选框4568 1 10新

复选框2345 1 25新

复选框4568 1 10新



来自上面的gridview我是更新我从gridview中选择前两行并在数据库中更新



String strConnString = ConfigurationManager.ConnectionStrings [ConnectionStrings] .ConnectionString;

SqlConnection con = new SqlConnection(strConnString);

con.Open();

foreach(grdRpt.Rows中的GridViewRow行)

{

CheckBox cb =(CheckBox)row.FindControl(chkselecdata);

if(cb.Checked == true)

{

int key = Convert.ToInt32(grdRpt.DataKeys [row.RowIndex] .Value);

SqlCommand cmd = new SqlCommand(UPDATE [transact]。[transaction_item] SET Status = '准备'其中transactee_id =+ key.ToString(),con);

cmd.ExecuteNonQuery();

}

}

con.Close();



i我正在使用c#
$ b / b来更新gridview所选行到数据库
在运行模式下
我在gridview中为id 1245选择第一行,但在数据库1245中为数据库中更新的两行选择。



i我没有选择gridview中的第二行

在运行模式下我只在gridview中选择第一行来更新在数据库中。但是在数据库中有两行1245正在更新。



上面代码中的错误是什么。





数据库输出我得到如下



ID数量价格状态



1245 2 50准备好

1245 1 25准备好



i我只选择gridview中的第一行。但是在数据库中为选择的id 1245更新了两行。





上面代码中的错误是什么。

In gridview record as follows

Selectdata ID Qty Price Status

checkbox 1245 2 50 New
checkbox 1245 1 25 New
checkbox 4568 1 10 New
checkbox 2345 1 25 New
checkbox 4568 1 10 New

From the above gridview i am updating i am selecting first two rows from the gridview and upate in the database

String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString(), con);
cmd.ExecuteNonQuery();
}
}
con.Close();

i am updating gridview selected row to database using c#

in the run mode i am selecting first row in the gridview for the id 1245 but in the database 1245 for the two rows updated in the database.

i am not selecting second row in the gridview
in the run mode i am selecting first row only in the gridview to update in the database. but in the database two rows 1245 are updating.

what is the mistake in my above code.


in the database ouput i get as follows

ID Qty Price Status

1245 2 50 Ready
1245 1 25 Ready

i am selecting only one first row in the gridview. but in database two rows updated for that selectd id 1245

what is the mistake in above code.

What I have tried:

In gridview record as follows

Selectdata ID Qty Price Status

checkbox 1245 2 50 New
checkbox 1245 1 25 New
checkbox 4568 1 10 New
checkbox 2345 1 25 New
checkbox 4568 1 10 New

From the above gridview i am updating i am selecting first two rows from the gridview and upate in the database

String strConnString = ConfigurationManager.ConnectionStrings["ConnectionStrings"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
foreach (GridViewRow row in grdRpt.Rows)
{
CheckBox cb = (CheckBox)row.FindControl("chkselecdata");
if (cb.Checked == true)
{
int key = Convert.ToInt32(grdRpt.DataKeys[row.RowIndex].Value);
SqlCommand cmd = new SqlCommand("UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString(), con);
cmd.ExecuteNonQuery();
}
}
con.Close();

i am updating gridview selected row to database using c#

in the run mode i am selecting first row in the gridview for the id 1245 but in the database 1245 for the two rows updated in the database.

i am not selecting second row in the gridview
in the run mode i am selecting first row only in the gridview to update in the database. but in the database two rows 1245 are updating.

what is the mistake in my above code.


in the database ouput i get as follows

ID Qty Price Status

1245 2 50 Ready
1245 1 25 Ready

i am selecting only one first row in the gridview. but in database two rows updated for that selectd id 1245.


what is the mistake in above code.

推荐答案

没有错。您的代码完全按照您的要求执行操作:更新ID为1245的所有记录。
There is no mistake. Your code is doing exactly what you tell it to do: UPDATE all records with id 1245.


看起来您的代码工作正常。

您的问题是你不能唯一地标识你的行,所以transactee_id为1245的每一行都会更新,以使你的工作正常,你需要一个只描述一行的唯一标识符(ID)。



It seems like your code is working fine.
Your Problem is that you can't uniquely identify your row, so every row with the transactee_id of 1245 is updated, to get this working as you expect you need a unique identifier (ID) that only describes one row.

"UPDATE [transact].[transaction_item] SET Status = 'Ready' Where transactee_id=" + key.ToString()





另外要提到的是如何将UPDATE字符串和参数组合在一起。

你这样做会导致多次意外错误甚至注入!

最好的方法是将'key'添加为参数(cmd.Parameters.AddWithValue();)



Another thing to mention is how you bring together your UPDATE string and your parameter.
The way you do it can cause multiple accidental errors or even injection!
The best way would be to add your 'key' as a Parameter (cmd.Parameters.AddWithValue();)


这篇关于如何使用C#更新数据库中gridview选中的复选框行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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