使用dataadapter更新gridview时并发冲突 [英] Concurrency Violation when updating gridview using dataadapter

查看:99
本文介绍了使用dataadapter更新gridview时并发冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试使用dataadapter和数据集更新gridview,但是在更改网格中的数据后单击保存按钮时,出现并发冲突错误.

代码如下

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);           
        }
    }
}



搜索任何帮助或建议将不胜感激,请在Google上搜索,但问题似乎出在我的代码上.



any help or suggestions will be appreciated, searched Google but it seems problem is something specific with my code.

推荐答案

我刚刚使用了mysqlcommandbuilder,并且运行正常.
I just used the mysqlcommandbuilder and it works fine.


尝试一下:

Try this:

radGridView2.DataSource = dtusers;
radGridview2.DataBind();


这篇关于使用dataadapter更新gridview时并发冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆