binding data to datagridview control

Hi Experts,
I have a datagridview control which populates data from mysqldb.
when user adds arecord, i am able to add the record back to db.
i'm not able to get the added record on the datagridview control when user presses the save button.
i am pasting the code for the save button, and the rebind method which binds datagridviewcontrol to the data.
should i need to use another dataadapter and dataset?
pls help me
private void btnSave_Click(object sender, EventArgs e)
            {
            try
                {
                conn = new MySqlConnection(connString);
                conn.Open();
                if (maskedTextBoxDate.Text != null)
                    {
                    MySqlCommand inCommand = new MySqlCommand("insert into client_notes(`notes_date`,`notes_message`,`client_id`) values(@pDate,@pMessage,@pId)", conn);
                    inCommand.Parameters.Add("@pDate", MySqlDbType.Date);
                    inCommand.Parameters["@pDate"].Value = Convert.ToDateTime(maskedTextBoxDate.Text.Trim());
                    inCommand.Parameters.Add("@pMessage", MySqlDbType.VarChar);
                    inCommand.Parameters["@pMessage"].Value = textBoxNotes.Text.Trim();
                    inCommand.Parameters.Add("@pId", MySqlDbType.Int32);
                    inCommand.Parameters["@pId"].Value = this.clNamecomboBox.SelectedValue;
 
                    inCommand.ExecuteNonQuery();
                    }
                }
            catch (Exception ex)
                {
                MessageBox.Show("error" + ex);
                }
            finally
                {
                maskedTextBoxDate.Clear();
                textBoxNotes.Clear();
                conn.Close();
                ReBind();
                }
 
            }
 
private void ReBind()
            {
             try
                {
                conn = new MySqlConnection(connString);
                conn.Open();
                MySqlCommand cmd = new MySqlCommand(@"select notes_date as Date,notes_message as Notes from client_notes where client_id=?cid ", conn);
                cmd.Parameters.Add("cid", MySqlDbType.Int32);
                cmd.Parameters["cid"].Value = clNamecomboBox.SelectedValue;
                dataGridViewNotes.AutoGenerateColumns = false;
                dataGridViewNotes.DataSource = cmd.ExecuteReader();
                dataGridViewNotes.Update();
                             
                }
            catch (Exception ex)
                {
                MessageBox.Show("Error " + ex);
                }
            finally
                {
                conn.Close();
                }
 
            }

Open in new window

APPIREDDYAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wht1986Commented:
I generally like to set the datasource of my gridview to a BindingSource, and the BindingSource to a dataTable.  I then use Table data adpters to perform the fill and updates on the data table.
0
wht1986Commented:
Sorry hit post too soon, heres a code example using a typed data set. You could also place the dataset and binding source on the form designer as well as just in code as ive shown
public partial class Form1 : Form
{
    public PMr3DataSet.SitesDataTable dataTable = null;
    public BindingSource bindingSource = null;
 
    public Form1()
    {
        InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        dataTable = new PMr3DataSet.SitesDataTable();
        bindingSource = new BindingSource(dataTable, "");
        this.dataGridView1.DataSource = bindingSource;
 
        PMr3DataSetTableAdapters.SitesTableAdapter adpt = new PMr3DataSetTableAdapters.SitesTableAdapter();
        adpt.Fill(dataTable);
    }
 
    private void button1_Click(object sender, EventArgs e)
    {
        PMr3DataSetTableAdapters.SitesTableAdapter adpt = new PMr3DataSetTableAdapters.SitesTableAdapter();
        adpt.Update(dataTable);
    }
}

Open in new window

0
APPIREDDYAuthor Commented:
sorry i am not able to understand ,can you explain it in my code view
0
wht1986Commented:
i was trying to show how i use a binding source to bind a grid to my dataset, and then how to update it. just trying to make things easier for you.

But lets look at what you have going on.  Basically you are saying the insert worked, but the grid never refreshes. Here are my thoughts

rather than binding to a data reader, try binding to a data table.  Im wondering if MySql is keeping some odd cache on the reader.

DataTable tbl = new DataTable();
tbl.Load(cmd.ExecuteReader());
conn.close();
dataGridViewNotes.DataSource = tbl;

If that still doesnt work but you are 100% sure the insert went in correctly, i would only call rebind when the connection from the insert actually closed

conn.StateChange += new StateChangeEventHandler(conn_StateChange);

void conn_StateChange(object sender, StateChangeEventArgs e)
{
    if (e.CurrentState == ConnectionState.Closed)
        this.ReBind();
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.