Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# access表中的Update方法仅适用于第一次调用,在下一次调用中仅复制第一个数据_C#_Ms Access_Datagridview - Fatal编程技术网

C# access表中的Update方法仅适用于第一次调用,在下一次调用中仅复制第一个数据

C# access表中的Update方法仅适用于第一次调用,在下一次调用中仅复制第一个数据,c#,ms-access,datagridview,C#,Ms Access,Datagridview,我的表单中有一个访问表,还有3个用于删除、更新和插入的按钮。 插入和删除工作正常。 我的问题是更新。它在第一次运行时运行良好,但当我选择datagridview的另一行时,它会使用最后一个数据(更新的第一个信息)更新该行 以下是更新的代码: com.Connection = con; com.CommandType = CommandType.Text; com.CommandText = "UPDATE login SET

我的表单中有一个访问表,还有3个用于删除、更新和插入的按钮。 插入和删除工作正常。 我的问题是更新。它在第一次运行时运行良好,但当我选择datagridview的另一行时,它会使用最后一个数据(更新的第一个信息)更新该行

以下是更新的代码:

    com.Connection = con;
                com.CommandType = CommandType.Text;
                com.CommandText = "UPDATE login SET [password]=@password , [uname]=@uname , [ufamily]=@ufamily , [admin]=@admin WHERE username='" + textBox1.Text + "'";

                com.Parameters.AddWithValue("@password", textBox2.Text);
                com.Parameters.AddWithValue("@uname", textBox3.Text);
                com.Parameters.AddWithValue("@ufamily", textBox4.Text);
                com.Parameters.AddWithValue("@admin", comboBox1.SelectedItem);
                con.Open();

                int result = com.ExecuteNonQuery();

                con.Close();

                if (result > 0)
                {
                    MessageBox.Show("success");
                    SelectAllRecords();
                }
                else
                {
                    MessageBox.Show("failed");
                }
这是我的问题:我第一次更新了第2行,它还可以。后来我更新了第4行,它只是从第2行复制了主键(username(
textbox1
))

更新后有一种刷新表格的方法:

      public void SelectAllRecords()
            {
                com.CommandType = CommandType.Text;
                com.CommandText = "SELECT * FROM login";
                com.Connection = con;
                con.Open();

                OleDbDataAdapter adapter = new OleDbDataAdapter(com);
                DataSet ds = new DataSet();

                adapter.Fill(ds, "login");
                dataGridView1.DataSource = null;


                dataGridView1.DataSource = ds.Tables["login"];
                dataGridView1.Columns[0].HeaderText = "شناسه کاربری";
                dataGridView1.Columns[1].HeaderText = "کلمه عبور";
                dataGridView1.Columns[2].HeaderText = "نام";
                dataGridView1.Columns[3].HeaderText = "نام خانوادگی";
                dataGridView1.Columns[4].HeaderText = "سطح دسترسی";
                dataGridView1.Columns[5].HeaderText = "آخرین بازدید";
                dataGridView1.Columns[6].HeaderText = "آخرین مطلب";
                con.Close();

            }
and event of dataGridView1_CellEnter:

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
            {
                if (dataGridView1.Rows.Count > 0)
                {
                   this.textBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
                   this.textBox2.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
                   this.textBox3.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
                   this.textBox4.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
                   if (dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[4].Value.ToString() == "مدیر")
                   {
                       this.comboBox1.SelectedIndex = 0;
                   }
                   else
                   {
                       this.comboBox1.SelectedIndex = 1;
                   }
                   this.textBox6.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[5].Value.ToString();
                   this.textBox7.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[6].Value.ToString();
                   username = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
                }
            }
编辑: 更多详细信息:
我选择该行并在文本框中输入值,然后单击“更新”按钮,它工作正常,然后我选择另一行并在文本框中输入值,但它会使用上次输入的值更新该行。

在更新查询中。请使用id更新数据。从网格视图中获取id

在哪种情况下调用更新代码?在按钮单击事件中,我猜问题出在datagaridview1\u cellenter事件中,但我找不到它。我认为您可以使用调试器解决此问题。在更新代码中放置一个断点,并检查文本框中存在的值。对不起,我太棒了。如何设置断点?