Question

How can i update TotalItems table, When I got some Items from TotalItems?

TotalItems: ID(int), ItemName(Nvarchar), StoreNumbers(int)      
Requests: RequestID(int), Requester(Nvarchar), ItemNumbers(int)

I used this, but do not work :

private void btnSave_Click(object sender, EventArgs e)
{
    string t;
    t = (gridItem.RowCount).ToString();

    db.DoCommand("insert into Requests(RequestID,Requester,ItemNumbers) values('" 
    + txtRequestID.Text + "','" + txtRequester.Text + "','" + txtItemNumbers.Text + "')");
    MessageBox.Show("Was Saved", "..", MessageBoxButtons.OK, MessageBoxIcon.Information);

    for (int i = 0; i < gridItem.RowCount - 1; i++)
    {
        a = gridItem[2, i].Value.ToString();
        b = int.Parse(gridItem[3, i].Value.ToString());

        dt = db.MySelect("select StoreNumbers from TotalItems where ItemName='" + a + "'");
        comboBox1.DataSource = dt;
        comboBox1.DisplayMember = "StoreNumbers";

        int count;
        int c;
        int.TryParse(comboBox1.Text, out c);
        count = c - b;

        db.DoCommand("update TotalItems set StoreNumbers='" + count.ToString() + "' where ItemName='" + a + "'");
    }
}

void show()
{
    DataTable dt = new DataTable();
    DataBase db = new DataBase();
    dt = db.MySelect("select * from Items where RequestID='" + txtRequestID.Text + "'");
    gridItem.DataSource = dt;
    gridItem.Columns[0].HeaderText = "ID";
    gridItem.Columns[1].HeaderText = "Request Code";
    gridItem.Columns[2].HeaderText = "Item Name";
    gridItem.Columns[3].HeaderText = "Number(s)";

    dt = db.MySelect("select StoreNumbers from TotalItems where ItemName='" + txtItemName.Text + "'");

    txtStoreNumbers.DataBindings.Clear();
    txtStoreNumbers.DataBindings.Add("text", dt, "StoreNumbers");
}

As i said, my problem persists. Example: I have 20 in 'StoreNumbers' column, when I get 2 Items from TotalItems, result must be 18 StoreNumbers in TotalItems so that it returns -2!, what's problem?

Was it helpful?

Solution

From comments, asking about where the database is:

It's part of my project

Then I will bet that the problem is that your looking at the table in database file in the project. The problem is, that isn't where the app executes: the app executes in bin/debug / bin/release (since this looks like a desktop application). When you build the project, the database file is copied into the execution location. When you run the application, it is therefore manipulating the copy of the database file. So: if you want to know if it has updated anything, you can't look at the database file in the project. You need to look at the copy of the database file in the execution folder.

Also:

  • ADO.NET parameters; use some
  • race condition on the fetch/update (you have no concurrency protection - with more than one caller, you will corrupt the data)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top