Click here to Skip to main content
16,014,392 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am using MySql Database; I retrieved data from database and populated data to text field and I have made some modifications on it. And I try to retrieve changes to DataTable by using data.GetChanges() command; it is retrieving null value! If I am changing the record position from current record to another record it is getting effected and I am getting the data changes by using same command. I think I have to execute some command before running GetChanges(). Please help me on this issue

Thanks & Regards
Thahir


Code :
C#
namespace App014
{
    public partial class Form1 : Form
    {
        private MySqlConnection conn;
        private DataTable data;
        private MySqlDataAdapter da;
        private MySqlCommandBuilder cb;

        public Form1()
        {
            InitializeComponent();
            ConnectDB();
        }

        private void ConnectDB()
        {
            if (conn != null)
                conn.Close();

            string connStr = "server=localhost;user id=root; password=xxxxx; database=eTest";
                

            try
            {
                conn = new MySqlConnection(connStr);
                conn.Open();

                data = new DataTable();

                da = new MySqlDataAdapter("SELECT code,name FROM USERMASTER", conn);
                cb = new MySqlCommandBuilder(da);

                da.Fill(data);

                lstUser.DataSource = data;
                lstUser.DisplayMember = "name";
                lstUser.ValueMember = "code";

                txtCode.DataBindings.Add(new System.Windows.Forms.Binding("Text", data, "code"));
                txtName.DataBindings.Add(new System.Windows.Forms.Binding("Text", data, "name"));

            }
            catch (MySqlException ex)
            {
                MessageBox.Show("Error connecting to the server: " + ex.Message);
            }
        }

        
        private void UpdateData()
        {
            DataTable changes = data.GetChanges();
            if (changes != null)
            {
                da.Update(changes);
                data.AcceptChanges();
            }
            else
            {
                MessageBox.Show("No Changes found");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            UpdateData();
        }        
    }
}
Posted
Updated 23-Jun-12 5:29am
v5
Comments
dsweera 23-Jun-12 5:19am    
can you update the question with code sample?

1 solution

go through this
Connecting to MySQL Database using C# and .NET[^]

you will find your answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900