Question

I have a Windows Form with 3 Gridviews (see screenshot)enter image description here. One of the gridviews gets a new value in an empty cell, the other two each get a new row. When clicking the save button I want to update the table that has the value added and insert the new rows into the other two tables. I have great difficulty figuring out the correct code. Right now I have:

private void buttonSave_Click(object sender, EventArgs e)
{
   DataTable table1 = alereDataSet.Tables["immaster"];
   DataTable table2 = uPCDataSet.Tables["UPC"];
   DataTable table3 = hangtagDataSet.Tables["upccode"];
   DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
   if (dr == DialogResult.Yes)
   try
   {
      immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.CurrentRows));
      upccodeTableAdapter.Update(table3.Select(null, null, DataViewRowState.Added));
      uPCTableAdapter.Update(table2.Select(null, null, DataViewRowState.Added));
   }

   catch (System.Exception ex)
   {
      System.Windows.Forms.MessageBox.Show(ex.Message);
   }
}

But when I click save nothing happens. I have a feeling it's because of my Update methods for the tables. Two of the three tables are VFP tables and the third is a SQL table. What I need help with is figuring out the Update Command. The two VFP tables won't create the Update method automatically and the code for the SQL table is:

UPDATE UPC
SET UPCBarcode = @UPCBarcode, UPCNumber = @UPCNumber, ItemNumber = @ItemNumber, Itemdescrip = @Itemdescrip
WHERE (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND 
                      (@IsNull_Itemdescrip = 1) AND (Itemdescrip IS NULL) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (@IsNull_ItemNumber = 1) AND 
                      (ItemNumber IS NULL) AND (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (@IsNull_ItemNumber = 1) AND (ItemNumber IS NULL) AND 
                      (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (@IsNull_UPCNumber = 1) AND (UPCNumber IS NULL) AND (ItemNumber = @Original_ItemNumber) AND 
                      (Itemdescrip = @Original_Itemdescrip) OR
                      (UPCBarcode = @Original_UPCBarcode) AND (UPCNumber = @Original_UPCNumber) AND (ItemNumber = @Original_ItemNumber) AND 
                      (Itemdescrip = @Original_Itemdescrip)

Not sure what else I should post. Please ask for any more code you think could be relevant.

Was it helpful?

Solution

Well, I guess after a week of nothing I finally was able to figure it out. Updating the SQL table was fairly easy since it creates its own UPDATE method when creating the Dataset. For the VFP tables I had to write my own UPDATE command which for the first table was

    UPDATE    immaster
    SET       item = ?, descrip = ?, upccode = ?
    WHERE     (item = ?)

and for the second VFP table was

UPDATE    upccode
SET              item = ?, upccode = ?, memoupc = ?
WHERE     (item = ?)

Then I had to write the event handler for the Save button:

//Save the data from the gridviews back to the respective tables
    private void buttonSave_Click(object sender, EventArgs e)
    {
        //Declare which tables get updated
        DataTable table1 = alereDataSet.Tables["immaster"];
        DataTable table2 = uPCDataSet.Tables["UPC"];
        DataTable table3 = hangtagDataSet.Tables["upccode"];
        DialogResult dr = MessageBox.Show("Are you sure you want to commit the values to the databases?", "Message", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
        if (dr == DialogResult.Yes)
            try
            {
                //Update immaster with the modified data
                immasterTableAdapter.Update(table1.Select(null, null, DataViewRowState.ModifiedCurrent));
                dataGridView1.Refresh();
                //Insert a new row in the upccode table with the values from the cells in the last row
                upccodeTableAdapter.Insert(dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView3.Rows[dataGridView3.Rows.Count - 1].Cells[1].Value.ToString(), ("Added " + System.DateTime.Now));
                dataGridView3.Refresh();
                //Insert a new row in UPC table with the values from the cells in the last row
                uPCTableAdapter.Insert(dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[3].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[2].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[0].Value.ToString(), dataGridView2.Rows[dataGridView2.Rows.Count - 1].Cells[1].Value.ToString());
                dataGridView2.Refresh();
                clearTextboxes();
            }
            catch (SqlException ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top