Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3998398
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:31:14+00:00 2026-05-20T07:31:14+00:00

I have stored procedure which update Customers and a datagridview to work with the

  • 0

I have stored procedure which update Customers and a datagridview to work with the data.
Editing not primary filed values works well, but if I try to update primary key filed value I get an exception “Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.”

This is my sp:

create procedure dbo.UpdateCustomers
@CustomerID nchar(5),
@CompanyName nvarchar(40),
@ContactName nvarchar(30),
@ContactTitle nvarchar(30),
@Address nvarchar(60),
@City nvarchar(15),
@Region nvarchar(15),
@PostalCode nvarchar(10),
@Country nvarchar(15),
@Phone nvarchar(24),
@Fax nvarchar(24)
as
update Customers
set CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address,
    City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax
where CustomerID = @CustomerID
go

And in code:

public void UpdateData()
            {
                using (var cn = new SqlConnection(_cs))
                {

                    ....

                    var updateCmd = new SqlCommand("UpdateCustomers", cn);
                    updateCmd.CommandType = CommandType.StoredProcedure;

                    updateCmd.Parameters.Add(
                        new SqlParameter("@CustomerID", SqlDbType.NVarChar, 5, "CustomerID"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@ContactName", SqlDbType.NVarChar, 30, "ContactName"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@ContactTitle", SqlDbType.NVarChar, 30, "ContactTitle"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@Address", SqlDbType.NVarChar, 60, "Address"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@City", SqlDbType.NVarChar, 15, "City"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@Region", SqlDbType.NVarChar, 15, "Region"));

                    ...

                    _da.UpdateCommand = updateCmd;
                    var count = _da.Update(_ds, "Customers"); //exception
                    GetData();
                }
            }

I understand that when this procedure executed with new CustomerID there is no rows affected to update. But, how can I pass from application new and old CustomerID?

Something like this in SQL:

update Customers set CustomerID = 'NEWVALUE' where CustomerID = 'BSBEV'
....

etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:31:14+00:00Added an answer on May 20, 2026 at 7:31 am

    You should never update the primary key.

    I suspect that you are getting this error because you are trying to update a row that does not exist.

    You may want to read up on Optimisitc Concurrency.

    If you want to change the Primary key, you should probably just insert a new record. What is your reason for doing this?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this stored procedure which get data from excell and then update status:
I have a stored procedure which is called inside a trigger on Insert/Update/Delete. The
I have a stored procedure which takes an XML parameter and inserts the data
I have a SQL Stored Procedure which is never ending because of some values
I have a stored procedure which looks like CREATE PROCEDURE Update-all ( @RowID INT,
I have an Oracle stored procedure which takes two parameters: a custom data type
I have a Stored procedure which schedules a job. This Job takes a lot
I have a stored procedure which executes a select statement. I would like my
I have a stored procedure which returns a ref cursor as follows: CREATE OR
I have a stored procedure which basically does something like select top 1 expiryDate,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.