当我使用C#更新SQL数据库时,正在添加一条新记录。 [英] When I am updating SQL database with C#, one new record is adding.

查看:269
本文介绍了当我使用C#更新SQL数据库时,正在添加一条新记录。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if I edited one value of one field and click on button for update one new record is adding not updating current record. Where I did wrong.If I touched value of that field only this is happining. Here is My code for updating 





.

SqlCommand cmd = new SqlCommand("update tempsales set [date]=@label2,[customername]=@label3,[billno]=@label4,[itemname]=@label5,[qty]=@label6,[price]=@label7,[tax]=@label8,[itemtot]=@label16 where [sno]=@label18", con);
                cmd.Parameters.Add("@label2", dateTimePicker1.Value);
                cmd.Parameters.Add("@label3", txtcust.Text);
                cmd.Parameters.Add("@label4", txtbno.Text);
                cmd.Parameters.Add("@label5", txtName.Text);
                cmd.Parameters.Add("@label6", txtQty.Text);
                cmd.Parameters.Add("@label7", txtPrice.Text);
                cmd.Parameters.Add("@label8", txtTax.Text);
                cmd.Parameters.Add("@label16", txtTotal.Text);//I wrote Add function on leave Event
                cmd.Parameters.Add("@label18", txtslno.Text);
                cmd.ExecuteNonQuery();





我尝试过:



对于这个字段LEAVE我只为INSERT编写了一个函数。无论如何都要在更新时禁用该函数。



What I have tried:

For this field LEAVE only I wrote one function for INSERT.Is there anyway to disable that function while updating.

推荐答案

如果您在文本框的Leave事件上编写了一个函数,并且该函数自动执行INSERT操作,则每次用户从该文本框切换到任何其他能够输入的控件时,都会调用该函数。



我们无法撤消当你在问题中显示的代码被执行时 - INSERT操作已经被操作到DB并插入了行。

猜测,您想要禁用自动INSERT - 完全删除Leave事件处理程序 - 并在用户说我已完成时手动检查是否需要INSERT或UPDATE上面的代码被调用。
If you wrote a function on the Leave event of a textbox, and that function does an INSERT operation automatically, then that function will be called each and every time the user switches from that textbox to any other control capable of taking input.

We can't "undo" that when the code you show in your question is executed - the INSERT operation has already been actioned to the DB and the row inserted.
At a guess, you want to disable the automatic INSERT - remove the Leave event handler completely - and manually check if an INSERT or UPDATE is necessary instead when the user says "I'm done" and the above code is called.


这篇关于当我使用C#更新SQL数据库时,正在添加一条新记录。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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