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.
I suspect
QTYinStockmatches more than one record in theStocktable. TheStocktable should have a primary key setup with an auto incrementing integer value so you can uniquely identify each record.Instead of:
you would use:
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.