【问题标题】:Edit & Update a record in Data grid View在数据网格视图中编辑和更新记录
【发布时间】:2011-03-21 18:43:47
【问题描述】:

我正在尝试通过转到 sqldatasource 属性“更新查询”并编写代码来更新我在 datagridview 中的数据,但它显示错误消息

“违反主键约束“PK_contact_master”。无法在对象“dbo.admission_table”中插入重复键。 该语句已终止。 "

请告诉我在datagridview中编辑数据并更新它的确切代码..

谢谢, 丘吉尔

更新:

Sql 代码:

Update admission_table 
set registration_id=@registration_id,
name_of_degree=@name_of_degree,
fees_paid=@fees_paid,
hostel=@hostel,
hostel_fees=@hostel_fees,
name_of_student=@name_of_student,
Dat­e_of_birth=@Date_of_birth,
nationality=@nationality,
gender=@gender,
address=@addres­s,
phone_no=@phone_no,
e_mail=@e_mail,
date=@date

【问题讨论】:

  • 向我们确切展示您现在拥有的代码,也许我们会这样做。
  • 更新 admission_table 设置registration_id=@registration_id,name_of_degree=@name_of_degree,fees_paid=@fees_paid,hostel=@hostel,hostel_fees=@hostel_fees,name_of_student=@name_of_student,Date_of_birth=@Date_of_birth,nationality=@nationality ,gender=@gender,address=@address,phone_no=@phone_no,e_mail=@e_mail,date=@date
  • registration_id 列是我的表的主键,等于@registration_id 之后是我的表列的名称

标签: c# asp.net sql-server-2005


【解决方案1】:

根据您提供的 sql 查询,您的问题似乎是您的更新查询中没有 where 子句,并且您似乎在更新中设置主键。

这意味着您将使用相同的信息更新表中的每条记录并违反 PK。

要解决此问题,我将首先添加一个 where 子句。一旦你只更新你想要的记录,你很可能会从你的 set 子句中删除 registration_id(假设那是你的 pk)。

【讨论】:

  • 嗯,非常感谢你,我已经解决了我的问题..它工作得很好..thank_u
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多