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

  • Home
  • SEARCH
  • 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 3406276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:36:29+00:00 2026-05-18T05:36:29+00:00

I’m trying to write a program that will change a particular GUID in a

  • 0

I’m trying to write a program that will change a particular GUID in a table to a GUID that the user specifies.
The issue is when it tries to overwrite the old GUID with the new one it will give me the following error:

You cannot add or change a record
because a related record is required
in table tblEF

The database has 5 tables. The main table, tblA, has a Primary Key set to pkAccounts. The other tables all have a Foreign Key, called fkAccounts.

All the relationships are set to Enforce Referential Integrity and Cascade Delete Related Records. If I manually open the database and edit the relationships to have Cascade Update Related Fields, my program will update the GUIDs BUT the program that the database is used in will no longer work.

Trying to overcome this I added in to variables that would drop the primary key on the main table then add the primary key back after the program finished replacing all the GUIDs. In this case I will get Syntax error in Alter Table

Here is my code. Sorry if it is messy but this is one of my first programs. Plus my first time messing around with SQL stuff.

try
 {
   string GetRI = "SELECT fkAccountGUID FROM tblR";
   string GetTWI = "SELECT pkAccountGUID FROM tblTW";
   string GetAI = "SELECT pkAccountGUID FROM tblA";
   string GetEAI = "SELECT fkAccountGUID FROM tblEAI";
   string GetEF = "SELECT fkAccountGUID FROM tblEF";
   string NoPK = "ALTER TABLE tblA DROP CONSTRAINT pkAID";
   string PK = "ALTER TABLE tblA ADD PRIMARY KEY (pkAID)";

   DataSet ds = new DataSet();

   //create a connection to the database
       OleDbConnection ConnectDatabase = new OleDbConnection(
           "Provider=Microsoft.Jet.OLEDB.4.0;" +
           @"Data Source=C:\db.mdb;" +
           "Persist Security Info=True;" +
           "Jet OLEDB:Database Password=123;");

   //open the connection to the database
   ConnectDatabase.Open();

   //creates an adapter and runs the string command from the database connection
   //it will then fill the information in the dataset in its own table
   OleDbDataAdapter DatabaseAdapter = new OleDbDataAdapter(GetRI, ConnectDatabase);
   DatabaseAdapter.Fill(ds, "tblR");
   OleDbDataAdapter DatabaseAdapter1 = new OleDbDataAdapter(GetAI, ConnectDatabase);
   DatabaseAdapter.Fill(ds, "tblA");
   OleDbDataAdapter DatabaseAdapter2 = new OleDbDataAdapter(GetTWI, ConnectDatabase);
   DatabaseAdapter.Fill(ds, "tblTW");
   OleDbDataAdapter DatabaseAdapter3 = new OleDbDataAdapter(GetEAI, ConnectDatabase);
   DatabaseAdapter.Fill(ds, "tblEAI");
   OleDbDataAdapter DatabaseAdapter4 = new OleDbDataAdapter(GetEF, ConnectDatabase);
   DatabaseAdapter.Fill(ds, "tblEF");

   //get old GUID
   Console.WriteLine("What is the current GUID?");
   string OldGUID = Console.ReadLine();
   char ap = '\x0027';
   OldGUID = ap + OldGUID + ap;

   //get new GUID
   Console.WriteLine("What is the new GUID name?");
   string NewGUID = Console.ReadLine();
   NewGUID = ap + NewGUID + ap;

   //test lines
   //Console.WriteLine(NewGUID);
   //Console.WriteLine(OldGUID);

   //UPDATE string to rename the old GUID to the New GUID
   string UpdateR = "UPDATE tblR SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;
   string UpdateA = "UPDATE tblA SET pkAccountGUID=" + NewGUID + "WHERE pkAccountGUID=" + OldGUID;
   string UpdateTW = "UPDATE tblTW SET pkfkAccountGUID=" + NewGUID + "WHERE pkfkAccountGUID=" + OldGUID;
   string UpdateEA = "UPDATE tblTW SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;
   string UpdateEF = "UPDATE tblEF SET fkAccountGUID=" + NewGUID + "WHERE fkAccountGUID=" + OldGUID;

   //create the variables to run the string commands
   OleDbCommand updatecmd0 = new OleDbCommand(UpdateF);
   OleDbCommand updatecmd1 = new OleDbCommand(UpdateR);
   OleDbCommand updatecmd2 = new OleDbCommand(UpdateEA);
   OleDbCommand updatecmd3 = new OleDbCommand(UpdateTW);
   OleDbCommand updatecmd4 = new OleDbCommand(UpdateA);
   OleDbCommand nocheckcmd = new OleDbCommand(NoPK);
   OleDbCommand checkcmd = new OleDbCommand(PK);

   //have the commands connect to the database
   nocheckcmd.Connection = ConnectDatabase;
   updatecmd0.Connection = ConnectDatabase;
   updatecmd1.Connection = ConnectDatabase;
   updatecmd2.Connection = ConnectDatabase;
   updatecmd3.Connection = ConnectDatabase;
   updatecmd4.Connection = ConnectDatabase;
   checkcmd.Connection = ConnectDatabase;

   //Run the commands
   nocheckcmd.ExecuteNonQuery();
   updatecmd0.ExecuteNonQuery();
   updatecmd1.ExecuteNonQuery();
   updatecmd2.ExecuteNonQuery();
   updatecmd3.ExecuteNonQuery();
   updatecmd4.ExecuteNonQuery();
   checkcmd.ExecuteNonQuery();

   //Dispose the adapter and close the connection to the database.
   DatabaseAdapter.Dispose();
   ConnectDatabase.Close();

   //console will display the string if everything completed
   Console.WriteLine("Success. Press any key to exit.");
   Console.Read();

 }
catch (OleDbException Error)
 {
   //when an error occurs display the error in the console
   Console.WriteLine(Error.Message);
   Console.Read();
 }

Basically, how do I edit the GUID fields among the 5 database without manually opening the database checking Cascade Update, running my program, then opening the database again and unchecking Cascade Update?

  • 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-18T05:36:30+00:00Added an answer on May 18, 2026 at 5:36 am

    Instead of dropping constraints, just add a new row to the table and copy the data over.

    1. Insert a new row with the GUID the user wants.
    2. Then update the row with the data from the old row.
    3. Change all foreign key references to the new row
    4. Delete the old row
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.