none
Updating a Form (PLEASE HELP!!!) RRS feed

  • Question

  • Hi,  I'm fairly new at programming in C#.  I'm practicing from this book called Microsoft C# Professional Projects...I would've looked on their website for some answers, but its mysteriously shutdown.

    Anyway, I have 2 forms, each has a edit button so once pushed the information that is binded to the table I've created in SQL Server is entered in the textboxes.  Here is the code for the 1st form:

    private void WorkerForm_Load(object sender, EventArgs e)

            {

                // TODO: This line of code loads data into the 'masterDataSet.Workers' table. You can move, or remove it, as needed.

                this.workersTableAdapter.Fill(this.masterDataSet.Workers);

            }

            private void btnEdit_Click(object sender, EventArgs e)

            {

                masterDataSet.Workers.Clear();         

                workersTableAdapter.Fill(masterDataSet.Workers);     

            }

            private void btnSave_Click(object sender, EventArgs e)

            {

                workersTableAdapter.Update(masterDataSet.Workers);                MessageBox.Show("The Worker Table is Updated");

            }

            private void btnCancel_Click(object sender, EventArgs e)

            {

                masterDataSet.Workers.Clear();

                workersTableAdapter.Fill(masterDataSet.Workers);

            }

            private void btnExit_Click(object sender, EventArgs e)

            {

                this.Close();

            }

        }

    The problem I noticed is that when I execute the program and push the edit button, the information from the textboxes do appear however, if I edit the information in the textboxes or add new information & push the 'Save' button the messagebox will say that the database is updated, but the new or edited data does NOT show up in the table or when I navigate through the information when I push the 'Back' or 'Previous' buttons.

    I'm having the same issues with this form too:

    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.SqlClient;

    namespace Customer_Maintenance_Project

    {

        public partial class CustomerForm : Form

        {

           

            public CustomerForm()

            {

                InitializeComponent();

            }

            private void CustomerForm_Load(object sender, EventArgs e)

            {

                //Clears the error messages when the form is loaded//

                errCustForm.SetError(textBox1, "");

                errCustForm.SetError(textBox2, "");

                errCustForm.SetError(textBox3, "");

                errCustForm.SetError(textBox4, "");

            }

              

            private void btnEdit_Click(object sender, EventArgs e)

            {

                //Clears records & fills the textbox w/the records from the database.//

                customerDataSet.Clear(); 

                customersTableAdapter.Fill(customerDataSet.Customers);

                CurrentPosition();

            }

                   

            private void btnSave_Click(object sender, EventArgs e)

            { 

                bool flag;

                flag = true;

                if (textBox1.Text == "")

                {

                    errCustForm.SetError(textBox1, "Please specify a valid car number.");

                    flag = false;

                }

                else

                    errCustForm.SetError(textBox1, "");

                if (textBox2.Text == "")

                {

                    errCustForm.SetError(textBox2, "Please specify a valid name.");

                    flag = false;

                }

                else

                    errCustForm.SetError(textBox2, "");

                if (textBox3.Text == "")

                {

                    errCustForm.SetError(textBox3, "Please specify a valid address.");

                    flag = false;

                }

                else

                    errCustForm.SetError(textBox3, "");

                if (textBox4.Text == "")

                {

                    errCustForm.SetError(textBox4, "Please specify a valid make.");

                    flag = false;

                }

                else

                    errCustForm.SetError(textBox4,"");

                if (flag == false)

                    return;

                else

                {

                    customersTableAdapter.Update(customerDataSet.Customers);

                    MessageBox.Show("Database updated.");

                    ClearForm();

                }

            }

        

            void ClearForm() //I created a method that clears the textboxs once the messageBox is clicked.//

            {

                textBox1.Clear();

                textBox2.Clear();

                textBox3.Clear();

                textBox4.Clear();

                textBox5.Clear();

            }

           

            private void btnCancel_Click(object sender, EventArgs e)

            {

                customerDataSet.Customers.Clear(); //Clears all the rows in the dataset//

            

                btnNext.BindingContext[customerDataSet, "Customers"].Position = 0;

                CurrentPosition();

            }

            private void btnExit_Click(object sender, EventArgs e)

            {

                this.Close();

            }

            private void btnPrevious_Click(object sender, EventArgs e)

            {

                btnPrevious.BindingContext[customerDataSet, "Customers"].Position -= 1;

                CurrentPosition();

                customersBindingSource.MovePrevious();

            }

            private void CurrentPosition()

            {

                int currentPosition, ctr;

                ctr = this.BindingContext[customerDataSet, "Customers"].Count;

                if (ctr == 0)

                {

                    textBox5.Text = "(There are no records in the Customer table.)";

                }

                else

                {

                    currentPosition = this.BindingContext [customerDataSet,"Customers"].Position + 1;

                    textBox5.Text = currentPosition.ToString() + " of " + ctr.ToString();

                }

            }

            private void btnNext_Click(object sender, EventArgs e)

            {

                btnNext.BindingContext[customerDataSet, "Customers"].Position += 1;

                CurrentPosition();

                customersBindingSource.MoveNext();

            }

        }

    Can someone PLEASE HELP ME!?!?

    • Moved by Alexander Sun Wednesday, April 18, 2012 7:26 AM Move to more appropriate forum (From:Visual C# Language)
    Tuesday, April 17, 2012 12:23 AM

Answers

All replies

  • I didn't see where you executed EndEdit on your binding source.  You may want to take a look at this link: http://msdn.microsoft.com/en-us/library/ms171933(v=vs.100).aspx

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Tuesday, April 17, 2012 1:36 PM
  • Can you check the RowState property of the rows in the data masterDataset.Workers table. The row state should be modified for update to work correctly.

    Thanks Gaurav

    Tuesday, April 17, 2012 11:21 PM
  • Hi Shurley2009,

    Welcome to the MSDN forum.

    Your problem is related to ADO.NET DataSet, so I will move your thread to corresponding forum for better support.

    Sorry for any inconvenience.

    Best Regards, 


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, April 18, 2012 7:25 AM