TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sharpie
NA
10
25.7k
Listview in winforms CRUD Access Database.
Mar 30 2012 4:07 AM
Hi,
I'm trying to insert, update and delete content in my listview in winforms application at same time my access database.
My puzzle is still missing some important pieces in the coding or wrong query ... I need to be able to use the functionality of the controls and not do it hardcoded.
I have a listview, 3 buttons and a textbox. What I want to do is by selecting a row being able to delete it or change it in the textbox (or another control, I'm open for improvement :-) ), being able if I click on insert, to insert a row and fill it, this is just an idea behind it, as I said, I'm open for suggestions. I've done this successfully in a not-winforms application, but this way I'm stuck and I have to use the listview, Access database (Northwind -Noordenwind in Dutch-) which is connected to my application and winforms.
Thank you very much for your detailed replies, I appreciate.
MY CODE:
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 LWTestNoordenwindView
{
public partial class Form1 : Form
{
private OleDbConnection Connection;
private DataTable dTable; //behoudt query resultaten
private DataSet dSet;
private OleDbDataAdapter dAdapter;
public Form1()
{
InitializeComponent();
this.listView1.Focus();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
Connection = new OleDbConnection();
Connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\LayerWise\Documents\Noordenwind.accdb";
Connection.Open();
dTable = new DataTable();
dSet = new DataSet();
dSet.Tables.Add(dTable);
dAdapter = new OleDbDataAdapter("select * from Klanten;", Connection); //select alle tabellen
dAdapter.Fill(dTable);
foreach (DataRow myRow in dTable.Rows)
{
listView1.Items.Add(myRow[0].ToString());
for (int i = 1; i < dTable.Columns.Count; i++)
{
listView1.Items[listView1.Items.Count - 1].SubItems.Add(myRow[i].ToString());
}
}
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butInsert_Click(object sender, EventArgs e)
{
try
{
string Insertstring = "";
OleDbCommand InsertCMD = new OleDbCommand(Insertstring);
InsertCMD.ExecuteNonQuery();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butUpdate_Click(object sender, EventArgs e)
{
try
{
OleDbCommand UpdateCMD = new OleDbCommand("UPDATE Klanten SET Bedrijf='dffgdg' WHERE Id = 1;", Connection);
OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder();
UpdateCMD.ExecuteNonQuery();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
private void butDelete_Click(object sender, EventArgs e)
{
foreach (ListViewItem RecordtoDelete in listView1.SelectedItems)
{
try
{
OleDbCommand DeleteCMD = new OleDbCommand("DELETE from Klanten where Id=3;", Connection);
listView1.Items.Remove(listView1.SelectedItems[0]);
DeleteCMD.ExecuteNonQuery();
RecordtoDelete.Remove();
}
catch (Exception error)
{
MessageBox.Show("FOUT:" + error.Message);
}
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Connection.Close();
}
}
}
Reply
Answers (
4
)
How to Create a Stored Procedure for Two Tables ?
Best way to load data