Click here to Skip to main content
15,916,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to update the gridview using the dataadapter and dataset, but when i click the save button after changing the data in the grid I get Concurrency Violation error.

The code is as follows

namespace app1
{
    public partial class Form1 : Form
    {
        MySql.Data.MySqlClient.MySqlConnection conn;
        MySqlDataAdapter adapter;
        DataSet ds;
     
       public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        public void radButton2_Click(object sender, EventArgs e)
        {
            string myConnectionString;           
            myConnectionString = "server=localhost;uid=root;" +
                "pwd=xxxx;database=ldis;";
            
            conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
             try
             {
                 conn.Open();
             }
             catch (MySql.Data.MySqlClient.MySqlException ex)
             {
                 MessageBox.Show(ex.Message);
             }

             DataTable dtusers = new DataTable() ;
             dtusers = Getusers();
             radGridView2.DataSource = dtusers;
               conn.Close();             
        }

        private DataTable Getusers()
        {
            string query = "select userid,password,user_group,status,expiry from users";
            adapter = new MySqlDataAdapter(query, conn);  // adapter declaration
            ds = new DataSet();
            adapter.Fill(ds);
            
            adapter.UpdateCommand = new MySqlCommand(
            "UPDATE users SET  password=@password, user_group=@user_group,status=@status  WHERE userid=@userid;", conn);
            adapter.UpdateCommand.Parameters.Add("@userid", MySqlDbType.VarChar, 25, "userrid");
            adapter.UpdateCommand.Parameters.Add("@password", MySqlDbType.VarChar, 45, "password");
            adapter.UpdateCommand.Parameters.Add("@user_group", MySqlDbType.VarChar, 45, "user_group");
            adapter.UpdateCommand.Parameters.Add("@status", MySqlDbType.VarChar, 10, "status");      
            adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None;

            return ds.Tables[0];
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            adapter.Update(ds);           
        }
    }
}


any help or suggestions will be appreciated, searched Google but it seems problem is something specific with my code.
Posted
Updated 15-Dec-11 4:50am
v2
Comments
Omersayeed 15-Dec-11 2:29am    
Can Someone look at it carefully, I know its a silly mistake on my part,I know everything on the code looks right. I need to resolve it urgently, I am stuck here

any suggestions to try.

regards
Omer

I just used the mysqlcommandbuilder and it works fine.
 
Share this answer
 
Comments
fjdiewornncalwe 15-Dec-11 10:49am    
Thanks for posting your solution. I wish more people would do that.
Try this:

radGridView2.DataSource = dtusers;
radGridview2.DataBind();
 
Share this answer
 
Comments
Omersayeed 15-Dec-11 2:10am    
This is a windows forms application, Databind does not work here. I am not sure if the cause is because of using the dataadapter and dataset in the button click event. Is the application creating one more instance of dataset ??

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