I have a form which I want to serve the purpose that the user can remove and edit (not add) rows from a database table, "Users". I have created a form and a DataSource in VS2010, the DataSource was created using the new DataSource wizard. From this I dragged and dropped the DataGridView for the Users table in the DataSource windowstrip to the form.
The issue I have is that when I run the application, the data will load into the DataGridView fine, but when i delete or edit a row and click save it does not update the database.
I am an novice user so I'm sure I doing something stupid or naive - do i need to add some sql calls in here?
Any ideas?
public partial class EditUsers : Form
{
public EditUsers()
{
InitializeComponent();
}
private void EditUsers_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'debenhamsProjectOfficeDatabaseDataSet.Users' table. You can move, or remove it, as needed.
this.usersTableAdapter.Fill(this.debenhamsProjectOfficeDatabaseDataSet.Users);
}
private void usersBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.usersBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.debenhamsProjectOfficeDatabaseDataSet);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
A much more flexible way to populate your datagrid view with your database is to use a connection string.
Right click on your project file in solution explorer and add an new item. Add a class and name it connection.cs. In here type
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Your_Project_Name
{
class Connection
{
string ConnectionString;
public Connection()
{
ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=C:/User/somefolder/Your_Database.accdb;Persist Security Info=False;";
}
public string getConnection()
{
return ConnectionString;
}
}
Now add using System.Data.OleDb;
to your headers at the top of your form1.
Now in public partial class EditUsers : Form
put
OleDbConnection connect = new OleDbConnection();
OleDbCommand command = new OleDbCommand();
OleDbDataReader reader;
Connection c;
Finally in private void EditUsers_Load
type in
c = new Connection();
connect.ConnectionString = c.getConnection();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.