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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:41:08+00:00 2026-05-31T05:41:08+00:00

first i want to say that this site has been a really big help

  • 0

first i want to say that this site has been a really big help to me in the past couple of weeks. Well to start, i have written a windows form app in C# with a textbox for filtering, a listview and two textboxes and two buttons one for adding new stock and one for updating.

The idea is to open the program, it displays all stock, you select the item you want to edit and it drops the values to two textboxes, where you can modify them and save it, or add new stock items.

i got some code off youtube for updating a SQLCE database and it worked fine, but what i don’t understand is the when i tried using this code on a different form, to update my stock list, it would not work properly. it would update the entire column instead of the selected row.

I have tried writing it from scratch and even copying the working code, but both give the same error. Here is the code that i’m using, its just to update the two fields in the database from values from two textboxes if stock was uploaded incorrectly

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;        
    using System.Data.SqlServerCe;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    SqlCeConnection cn = new SqlCeConnection("Data source = c:\\Clients.sdf");
    string QTYinStock;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
            ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.ExitThread();

        }
    }
    private void button2_Click(object sender, EventArgs e)
    {

        if (checkBox1.Checked == true)
        {

            //Check the QTY 
            int QTY;
            bool isInteger = int.TryParse(textBox2.Text, out QTY);



            if (isInteger)
            {
                SqlCeCommand cm = new SqlCeCommand("INSERT INTO Stock(QTY_in_Stock, Cartridge_Code) VALUES(@QTY_in_Stock, @Cartridge_Code)", cn);
                cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text);
                cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text);


                try
                {
                    int affectedRows = cm.ExecuteNonQuery();

                    if (affectedRows > 0)
                    {
                        MessageBox.Show("Insert Successfull!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                        ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

                    }

                    else
                    {
                        MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);


                }
            }
            else
            {
                MessageBox.Show("No data inserted! The QTY is not valid!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);


            }
        }
        else 
        {
            MessageBox.Show("Please ensure all details are correct and check the checkbox");
        }
    }

    private void Texboxes_TextChanged(object sender, EventArgs e)
    {
        if (textBox2.Text != "" && textBox3.Text != "")
        {
            button1.Enabled = true;
            button2.Enabled = true;

        }

        else
        {
            button1.Enabled = false;
            button2.Enabled = false;
        }
    }
    public void ReadStock(SqlCeCommand cm)
    {

        listView1.Items.Clear();

        SqlCeDataReader dr3 = cm.ExecuteReader();

        while (dr3.Read())
        {

            ListViewItem it3 = new ListViewItem(dr3["QTY_in_Stock"].ToString());
            it3.SubItems.Add(dr3["Cartridge_Code"].ToString());




            listView1.Items.Add(it3);
        }
        dr3.Close();
        dr3.Dispose();

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        ReadStock(new SqlCeCommand("SELECT * FROM Stock WHERE Cartridge_Code LIKE '%" + textBox1.Text + "%' ORDER BY Cartridge_Code", cn));

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {

            int QTY;
            bool isInteger = int.TryParse(textBox2.Text, out QTY);

            if (isInteger)
            {
                SqlCeCommand cm = new SqlCeCommand("UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE QTY_in_Stock = '" + QTYinStock +"'", cn);
                cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text);
                cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text);



                try
                {
                    cm.ExecuteNonQuery();
                    ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

                    textBox2.Text = textBox3.Text = "";


                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);


                }

                checkBox1.Checked = false;
            }
            else
            {

                MessageBox.Show("The QTY Is Not Valid", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }
        else
        {
            MessageBox.Show("Please ensure all Data is correct and checkbox is Checked");
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {

            QTYinStock = textBox2.Text = listView1.SelectedItems[0].Text;
            textBox3.Text = listView1.SelectedItems[0].SubItems[1].Text;


        }

        else
        {
            textBox2.Text = textBox3.Text = "";


        }
    }
    }
    }

sorry for posting the entire code and i know its not coded neatly, I’ve only been coding for about a month if this is only a syntax error could you please point it out since I’ve been staring at this for almost a day and cant figure it out, or any other ideas for doing this much appreciated.

  • 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-31T05:41:10+00:00Added an answer on May 31, 2026 at 5:41 am

    I suspect QTYinStock matches more than one record in the Stocktable. The Stock table should have a primary key setup with an auto incrementing integer value so you can uniquely identify each record.

    Instead of:

    UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code =
    @Cartridge_Code WHERE QTY_in_Stock = ‘” + QTYinStock +”‘”

    you would use:

    UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE StockID = " + stockID
    

    stockID is the unique row identifier held in a variable. You will need to retireve this value sometime when you load the records. You should also consider making the variable in the WHERE clause a paramter just like the @QTY_in_Stock and @Cartridge_Code values.

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

Sidebar

Related Questions

First I want to say thanks to everyone that reads this. Stackoverflow is an
First off, I want to say this site is AWESOME! and it helped me
First thing i want to say that it's not an easy question to explain,
First off I want to say that I am not using threads or multiple
First of all i want to say that i have searched each and every
First of all i want to say that I'm still a beginner in ASP.NET
First, I just want to say that the stackoverflow community is empowering to say
Let’s say that my string is: test! I want to get the first character
At first I want to say: I couldn't find a support forum for this
First , I want to thank all the persons who works for this site,

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.