[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Datagrid refresh issue

Hi all,

I'm using VB.NET (VS 2003) and SQL Server 2000.

I am running the following select command upon opening my form so to populate a datagrid:

        SqlConnection1.Open()
        SqlCommand1.Parameters("@Cont").Value = "" & PUB_Cont & ""
        SqlCommand1.Parameters("@Str").Value = "" & PUB_Str & ""
        SDA_plates.SelectCommand = SqlCommand1
        SqlCommand1.ExecuteNonQuery()
        SDA_plates.Fill(DS_plates1)

This works fine. However, when I add a row of data to the datagrid I run the following code which fails:

SDA_plates.Update(DS_plates1.GetChanges())
        SqlConnection1.Open()
        SqlCommand1.Parameters("@Cont").Value = "" & PUB_Cont & ""
        SqlCommand1.Parameters("@Str").Value = "" & PUB_Str & ""
        SDA_plates.SelectCommand = SqlCommand1
        SqlCommand1.ExecuteNonQuery()
        SDA_plates.Fill(DS_plates1)

I get the following error:

An unhandled exception of type 'System.Data.ConstraintException' occurred in system.data.dll

Additional information: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.


This would indicate that I am breaking a constraint set on the database table? However, when I look in the table all data on the row in question looks fine. Infact, when I go back into the form it opens fine showing the relevant row (no error). I do have a few formulas working in the table which calculate costs, areas, weights etc... from data held in other fields... Could this be causing the problem?

Cheers
0
FMabey
Asked:
FMabey
  • 5
  • 2
1 Solution
 
FMabeyAuthor Commented:
I have now tested the code with all formula removed and all fields allowing nulls (apart from the 1 key field) and I still get the error. This seems to indicate that the key field is the problem. But it is not breaking any unique constraint rules.

Any ideas anyone?
0
 
NickGeorghiouCommented:
Hi FMabey,

Do you start with an empty database everytime you run the app? Is it simply that you are trying to add the same row to the database that is already in there? This would cause primary key error.

Delete the row from the database and then re-run...?

Nick
0
 
Bob LearnedCommented:
Try this:

DS_plates1.EnforceConstraints = False
SDA_plates.Fill(DS_plates1)
DS_plates1.EnforceConstraints = True

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
FMabeyAuthor Commented:
The actual update itself is running fine. It's when I try and reload the datagrid that I'm gettting the error. If I come out of the form and let exaclty the same code run upon entry, the datagrid loads fine with exactly the same data.
0
 
FMabeyAuthor Commented:
Bob,

I still get the error... This time when I turn the contraints back on!
0
 
Bob LearnedCommented:
Right, it's because you haven't cleared the DataSet, before calling the Fill again, so you get the same records again.

Bob
0
 
FMabeyAuthor Commented:
How do I clear the dataset?
0
 
FMabeyAuthor Commented:
Excellent, thanks Bob.

I used DS_plates1.clear() and it worked a treat!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now