I need to be able to insert new rows, edit and delete and save to the gridview and also the database. It doesn't work a 100%.
Anyone has some advice?
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
OleDbConnection con = new OleDbConnection();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\\Northwind 2007.accdb";
con.Open();
ds.Tables.Add(dt);
da = new OleDbDataAdapter("SELECT * from Customers", con);
da.Fill(dt);
dg.DataSource = dt.DefaultView;
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butUpdate_Click(object sender, EventArgs e)
{
try
{
da.UpdateCommand = new OleDbCommand("UPDATE Customers SET Company='test' WHERE ID = 1;", con);
con.Open();
da.UpdateCommand.ExecuteNonQuery();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butDelete_Click(object sender, EventArgs e)
{
try
{
da.DeleteCommand = new OleDbCommand("DELETE from Customers where Id=3;", con);
con.Open();
da.DeleteCommand.ExecuteNonQuery();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butInsert_Click(object sender, EventArgs e)
{
try
{
da.InsertCommand = new OleDbCommand("INSERT INTO Customers (Company, Last Name) VALUES 'Test','TestName';", con);
con.Open();
da.InsertCommand.ExecuteNonQuery();
//string Insertstring = "";
//OleDbCommand InsertCMD = new OleDbCommand(Insertstring);
//InsertCMD.ExecuteNonQuery();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
con.Close();
}
}
}
Sam HobbsPosted Apr 2, 2012, 5:49 PM
What is the error information? What is the source code and/or SQL that is causing an error?
SharpiePosted Apr 2, 2012, 4:46 PM
SharpiePosted Apr 2, 2012, 4:41 PM
Satyapriya NayakPosted Apr 2, 2012, 3:25 AM
Try this...
Add Update delete in datagridview
App.xml file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Update_delete_datagridview
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
OleDbCommandBuilder olcb;
DataTable dataTable;
BindingSource bindingSource;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "SELECT * FROM student";
oledbda = new OleDbDataAdapter(str, con);
olcb = new OleDbCommandBuilder(oledbda);
dataTable = new DataTable();
oledbda.Fill(dataTable);
con.Close();
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
dataGridView1.DataSource = bindingSource;
dataGridView1.Columns[0].Visible = false;
}
private void btnaddupdate_Click(object sender, EventArgs e)
{
try
{
oledbda.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
MessageBox.Show("Records updated");
}
private void btndelete_Click(object sender, EventArgs e)
{
try
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
oledbda.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
MessageBox.Show("Records Deleted");
}
}
}
Thanks
If this post helps you mark it as answer
Sam HobbsPosted Apr 2, 2012, 2:42 AM