Avatar of J. Ross Hughson
J. Ross Hughson
Flag for Canada asked on

Why am I getting a duplicate key value error when it isn't?

First, let me say that I'm very new to vb.net programming and especially working with MS SQL Server. Here's my problem:
The code in the attached file works well when creating a new row with new data. I then forced an error to test my "Catch" statement and it worked as well. My problem is that I then changed one of the text boxes from which the key value is created but I'm still getting the duplicate key value error. I used MessageBox to show that it was changing the key value so I don't understand what's happening. Do I need to initialize the Data Adapter or Data Table before I run this each time? Please keep any explanations simple enough for a newbie to understand.
Visual Basic.NET

Avatar of undefined
Last Comment
J. Ross Hughson

8/22/2022 - Mon
Scott McDaniel (EE MVE )

Without knowing more about your table setup it's hard to say, but you need to find out the actual error you're getting instead of returning your own. Try this in the Catch handler:

Catch dbException As System.Data.SqlClient.SqlException
            boolCreateBinderOK = False
            MessageBox.Show("SQLException: " & dbException.ToString)

Catch ex As Exception
            boolCreateBinderOK = False
            MessageBox.Show("Exception: " & ex.ToString)

Run it and report back what the issue is.
J. Ross Hughson

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Scott McDaniel (EE MVE )

You really should Delete this question, since no solution was provided. I'm objecting, which will allow you to Delete the question instead of accepting a non-solution as the answer.
J. Ross Hughson

There were no solutions offered that dealt with the problem.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes