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 7402633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:47:30+00:00 2026-05-29T04:47:30+00:00

I have a database with some columns named UPC title Description and Quantity. i

  • 0

I have a database with some columns named UPC title Description and Quantity. i am trying to do a check to see if a UPC number already exists in the column and IF IT DOES then update the quantity of that entry by 1 here is what i have so far:

    MySqlConnection connection = new MySqlConnection(MyConString);
                connection.Open();
                MySqlCommand command = connection.CreateCommand();
                command.CommandText = "INSERT INTO tableName " +
                           "(upc, title, description, quantity) " +
                       "VALUES " +
                           "(@upc, @title, @description, @quantity)";
                MySqlCommand upcCheck = connection.CreateCommand();
                upcCheck.CommandText = "Select COUNT (*) FROM tableName WHERE " +
                    "(upc=@upc) LIMIT 1";
                upcCheck.Parameters.AddWithValue("@upc", upc.Text); //Checks for duplicate UPCs
                MySqlDataReader check = upcCheck.ExecuteReader();
                    if( check.Read() != false)
                    {
                        command.Parameters.AddWithValue("@quantity",++); //not sure how to word it right
                    }


                command.Parameters.AddWithValue("@upc", upc.Text); //adding parameters SAFELY to the statement
                command.Parameters.AddWithValue("@title", titlename);
                command.Parameters.AddWithValue("@description", descname);
                command.Parameters.AddWithValue("@quantity", "1");

                MySqlDataReader result = command.ExecuteReader();

I’m just not sure how to word it to do the check.

UPDATE! Here is the working code.

    MySqlConnection connection = new MySqlConnection(MyConString);

                connection.Open();
                MySqlCommand updatecommand = connection.CreateCommand();

                updatecommand.CommandText = "UPDATE tableName Set quantity = quantity +1 " +
                    "WHERE upc = @upc";
                updatecommand.Parameters.AddWithValue("@upc", upc.Text);
                using (MySqlDataReader updateResult = updatecommand.ExecuteReader())
                {


                    if (updateResult.RecordsAffected == 0)
                    {
                        affected = true;
                    }
                    else
                    {
                        affected = false;
                    }
                }
                 if(affected == true)
                {

                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "INSERT INTO tableName " +
                        "(upc, title, description, quantity) " +
                    "VALUES " +
                        "(@upc, @title, @description, @quantity)";
                    command.Parameters.AddWithValue("@upc", upc.Text);
                    command.Parameters.AddWithValue("@title", titlename);//adding parameters SAFELY to the statement
                    command.Parameters.AddWithValue("@description", descname);
                    command.Parameters.AddWithValue("@quantity", "1");



                    MySqlDataReader result = command.ExecuteReader();
                }
  • 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-29T04:47:31+00:00Added an answer on May 29, 2026 at 4:47 am

    Although I’ve not worked specifically with MySqlDataReader, I would slightly change the queries around… the otherwise format (parameterization) you have for the insert is completely fine.

    Although the ON DUPLICATE KEY UPDATE provided by Hristo is cool, you would be missing the other column elements for your record. I would change to..

    MySqlCommand command = connection.CreateCommand();
    command.CommandText = "UPDATE tableName Set Quantity = Quantity +1 " +
                               "WHERE upc = @upc"
    command.Parameters.AddWithValue("@upc", upc.Text);
    

    ** Dont know how the value / status will be returned from the MySqlDataReader, but

    result = command.ExecuteReader();
    
    if( the status indicates it actually succeeded in an update, 
        or how many records were updated > 0 )
      return;  // you've just increased the counter...
    

    OTHERWISE, it was not on file.. continue with the rest of your SQL-insert command, we can overwrite the last command instance…

    command = connection.CreateCommand();
    command.CommandText = "INSERT INTO tableName " +
                               "(upc, title, description, quantity) " +
                           "VALUES " +
                               "(@upc, @title, @description, @quantity)";
    
    command.Parameters.AddWithValue("@upc", upc.Text);
    command.Parameters.AddWithValue("@title", titlename);
    command.Parameters.AddWithValue("@description", descname);
    command.Parameters.AddWithValue("@quantity", "1");
    
    result = command.ExecuteReader();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have one database, and it contains some columns. My requirement is that how
I've got a set of database results where some of the columns have empty
I have some columns in a few tables in my database that are all
I am using MyBatis 3.0.3 and have problem: some columns in database have names
I have many columns in oracle database and some new are added with values.
I have a database with some tables and columns. I want to create a
I have some files stored in a database blob column in Oracle 9. I
I have a huge database with some 100 tables and some 250 stored procedures.
I have an existing database containing some pictures in blob fields. For a web
I have some data files to import into a database with some unique delimiters:

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.