使用C# - plz help的datagirdview时,更新查询失败 [英] Updation query fails in using datagirdview of C# - plz help

查看:78
本文介绍了使用C# - plz help的datagirdview时,更新查询失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好



我在Windows应用程序的保存按钮中使用以下代码

of c#。我正在使用带有已定义列的dataGridview来输入数据。

我正在使用datagirdview循环将数据插入并更新到表中。

我能够插入数据使用insert命令

但是在编辑了datagridview中的记录并修改了行之后,

这个下面的代码更新了表的所有记录,只有最后一行datagridview并没有错误消息。 PLZ有助于解决这个问题。



感谢和问候



Madhu





con.Open();



foreach(DataGridViewRow dataGridView1.Rows中的行)

{

cmd =新的SqlCommand(UPDATE ORDER_DETL SET PROD_DESC = @ b,quality = @ c,sizecd = @ d

其中order_no = @a ,con);

{



尝试

{

cmd。 Parameters.AddWithValue(@ a,textBox1.Text);

cmd.Parameters.AddWithValue(@ b,row.Cells [prod_desc]。Value);

cmd.Parameters.AddWithValue(@ c,row.Cells [quality]。Value);

cmd.Parameters.AddWithValue(@ d,row.Cells [ sizecd]。值);

cmd.ExecuteNonQuery();



}



catch(例外情况)

{



}



}

}



MessageBox.Show(记录更新);



我尝试过:



我假设它没有在网格行之间导航我尝试了以下内容。

但是它没有用。



int currentRow = dataGridView1.SelectedRows [0] .Index;

if(currentRow< dataGridView1.RowCount)

{

dataGridView1.Rows [++ currentRow] .Selected = true;

}

解决方案

 其中 order_no =  @ a  

 cmd.Parameters.AddWithValue(  @ a ,textBox1.Text); 



textBox1 中的文本在循环迭代之间不会改变。你每次都在更新同一行。



猜测,你应该从当前行中提取值,就像你正在为其他参数做的那样。 / BLOCKQUOTE>

Hi

I am using the following code in "Save" Button of my windows application
of c#. I am using dataGridview with defined columns to enter the data.
I am using for loop of datagirdview to insert and update the data into table.
I am able to insert the data using the insert command
but after editing the records in datagridview and after modify the rows,
this following code updates all the records of the table with only last row of the datagridview and there is no error message. plz help to solve this.

thanks and regards

Madhu


con.Open();

foreach (DataGridViewRow row in dataGridView1.Rows)
{
cmd = new SqlCommand("UPDATE ORDER_DETL SET PROD_DESC = @b,quality =@c,sizecd=@d
where order_no = @a", con);
{

try
{
cmd.Parameters.AddWithValue("@a", textBox1.Text);
cmd.Parameters.AddWithValue("@b", row.Cells["prod_desc"].Value);
cmd.Parameters.AddWithValue("@c", row.Cells["quality"].Value);
cmd.Parameters.AddWithValue("@d", row.Cells["sizecd"].Value);
cmd.ExecuteNonQuery();

}

catch (Exception ex)
{

}

}
}

MessageBox.Show("record updated");

What I have tried:

I assumed that it is not navigating between the rows of grid I tried the following.
But it is not working.

int currentRow = dataGridView1.SelectedRows[0].Index;
if (currentRow < dataGridView1.RowCount)
{
dataGridView1.Rows[++currentRow].Selected = true;
}

解决方案

where order_no = @a

cmd.Parameters.AddWithValue("@a", textBox1.Text);


The text in textBox1 does not change between iterations of your loop. You are updating the same row every time.

At a guess, you should be pulling the value from the current row, as you are doing for the other parameters.


这篇关于使用C# - plz help的datagirdview时,更新查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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