Inserting,deleting,updating and editing records to a MS Access database

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 11
Reputation: omoz4real is an unknown quantity at this point 
Solved Threads: 0
omoz4real omoz4real is offline Offline
Newbie Poster

Inserting,deleting,updating and editing records to a MS Access database

 
0
  #1
Oct 30th, 2007
Hi everyone,
please i am writing a program in C# for connecting to a microsoft access database using visual studio 2005 and ADO.NET. i want to be able to use Dataset to update,delete,insert and edit records in the database.please how do i go about this.I already have aform with a list box,a text box and four buttons for inserting,updating,deleting, and inserting records but i havent been able to get the connection to the database. i am a little bit confused if have to use a connection string and a dataAdapter to get a connection to the database everytime i want to perform each action like updating, deleting e.t.c and please how do i write the cade for the update,delete,edit and insert statements as i am a little bit new to ADO.NET and c# platform.below is the code i have written so far but it dosent work. can anyone please help me in modifying/correcting the codes.the the name of my access database is BookCSharp with a table name Books.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace DBConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void button5_Click(object sender, EventArgs e)
{

}

private void button4_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string dbconnection ="Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";

string dbcommand = "INSERT into Books (Title, Pages, AuthorBook) " + " VALUES ('C# good','300',OmosJigga)";

OleDbDataAdapter DataAdapterTest = new OleDbDataAdapter (dbcommand, dbconnection);

DataSet BooksDataSet = new DataSet();

DataAdapterTest. Fill(BooksDataSet);

DataTable dtBooks = BooksDataSet.Tables[0];
}

private void button2_Click(object sender, EventArgs e)
{
string dbconnection2 = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=BookCSharp.mdb";
string command2 = "UPDATE Books SET Title = 'Omos' WHERE Publisher = 'O,Rielly'";
OleDbDataAdapter Data2 = new OleDbDataAdapter(command2,dbconnection2);
DataSet b = new DataSet();
Data2.Fill(b);
}

private void button3_Click(object sender, EventArgs e)
{
string dbconnection1 = "Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";
string command1 = "Delete from Books where (Title = ? AND Pages = ? AND AuthorKey = ? AND Publisher = ?)";
OleDbDataAdapter Data1 = new OleDbDataAdapter(command1,dbconnection1);
DataSet a = new DataSet();
Data1.Fill(a);
DataTable aBook = a.Tables[0];

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

}
}

thanks.
OmosJiggaTM
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: khanhtrung is an unknown quantity at this point 
Solved Threads: 0
khanhtrung khanhtrung is offline Offline
Newbie Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #2
Oct 31st, 2007
Hi omoz4real.
In your code,i found that you hadn't opened your connection before executed a command,so you probably couldn't connect to the database.
If you didn't intend to execute a command which return a table as a result,you should use another way.There is xxCommand.ExecuteNonQuery().
I have a small attached program that clear your confuse.
Hope this useful.
Attached Files
File Type: zip MSAccessDB.zip (40.8 KB, 479 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 172
Reputation: Jugortha is an unknown quantity at this point 
Solved Threads: 16
Jugortha Jugortha is offline Offline
Junior Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #3
Oct 31st, 2007
Originally Posted by omoz4real View Post
Hi everyone,
please i am writing a program in C# for connecting to a microsoft access database using visual studio 2005 and ADO.NET. i want to be able to use Dataset to update,delete,insert and edit records in the database.please how do i go about this.I already have aform with a list box,a text box and four buttons for inserting,updating,deleting, and inserting records but i havent been able to get the connection to the database. i am a little bit confused if have to use a connection string and a dataAdapter to get a connection to the database everytime i want to perform each action like updating, deleting e.t.c and please how do i write the cade for the update,delete,edit and insert statements as i am a little bit new to ADO.NET and c# platform.below is the code i have written so far but it dosent work. can anyone please help me in modifying/correcting the codes.the the name of my access database is BookCSharp with a table name Books.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;

namespace DBConnection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{

}

private void button5_Click(object sender, EventArgs e)
{

}

private void button4_Click(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
string dbconnection ="Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";

string dbcommand = "INSERT into Books (Title, Pages, AuthorBook) " + " VALUES ('C# good','300',OmosJigga)";

OleDbDataAdapter DataAdapterTest = new OleDbDataAdapter (dbcommand, dbconnection);

DataSet BooksDataSet = new DataSet();

DataAdapterTest. Fill(BooksDataSet);

DataTable dtBooks = BooksDataSet.Tables[0];
}

private void button2_Click(object sender, EventArgs e)
{
string dbconnection2 = "Provider=Microsoft.Jet.OLEDB.4.0;" + @"data source=BookCSharp.mdb";
string command2 = "UPDATE Books SET Title = 'Omos' WHERE Publisher = 'O,Rielly'";
OleDbDataAdapter Data2 = new OleDbDataAdapter(command2,dbconnection2);
DataSet b = new DataSet();
Data2.Fill(b);
}

private void button3_Click(object sender, EventArgs e)
{
string dbconnection1 = "Provider=Microsoft.Jet.OLEDB.4.0;" +@"data source=BookCSharp.mdb";
string command1 = "Delete from Books where (Title = ? AND Pages = ? AND AuthorKey = ? AND Publisher = ?)";
OleDbDataAdapter Data1 = new OleDbDataAdapter(command1,dbconnection1);
DataSet a = new DataSet();
Data1.Fill(a);
DataTable aBook = a.Tables[0];

}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

}
}

thanks.
Ok, I give u details or a road map about ADO.Net I think it's interessant to know that from at first.
At contrast of the ADO plus where the connected mode is only connected , In the ADO.NET there are two modes, namely, the connected mode and the disconnected mode. In the first mode we use the connection and the Datareader, those two objects provide us a direct connection to the database. The data reader can only read data by moving forward you can make direct update only by using the ExecuteNonQuery method of the command objet. The second way I mean the disconnected use a data connection dataAdapter and a Dataset
When the connection is established and the method Fill of the dataAdpter is called the last one fill all desired data from data base in the dataset wirch is an xml data representation
then the connection is closed. The Ado dot net is using this technic to enhance the securities connections and to optimise the situation in a multi connection environnement. because the number of connection can decrease the application performances.
By the way a thing you have a mistake when writting the connection string you forgot the root C:\\dataBaseName.mdb instead of dataBaseName.mdb in data source
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 5
Reputation: khanhtrung is an unknown quantity at this point 
Solved Threads: 0
khanhtrung khanhtrung is offline Offline
Newbie Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #4
Nov 2nd, 2007
Yes.
I agree with Jugortha about two connection mode.And i also agree with him about what doing for each connected type.I had noticed these details in my attached program(readme.txt).
Thank for make it clearly.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 20
Reputation: BstrucT is an unknown quantity at this point 
Solved Threads: 1
BstrucT's Avatar
BstrucT BstrucT is offline Offline
Newbie Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #5
Jul 22nd, 2008
Just stumbled upon this old thread.

My thanks for the example attachment program.
I have been searching for so long.

Thanks

>BstrucT
Hardware Expert / Programming Jnr
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: pradeep_deepu07 is an unknown quantity at this point 
Solved Threads: 0
pradeep_deepu07 pradeep_deepu07 is offline Offline
Newbie Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #6
Feb 6th, 2009
when i try to inert the data to access from vb it will throw an exception " Data type mismatch in criteria expression" this exception wiill thrown in the line " DataAdapterTest.Fill(BooksDataSet)" whats the solution for this how to overcomethis problem please help me..
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: pradeep_deepu07 is an unknown quantity at this point 
Solved Threads: 0
pradeep_deepu07 pradeep_deepu07 is offline Offline
Newbie Poster

Re: Inserting,deleting,updating and editing records to a MS Access database

 
0
  #7
Feb 6th, 2009
when i try to inert the data to access from vb it will throw an exception " Data type mismatch in criteria expression" this exception wiill thrown in the line " DataAdapterTest.Fill(BooksDataSet)" whats the solution for this how to overcomethis problem please help me..
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: badshahronak is an unknown quantity at this point 
Solved Threads: 0
badshahronak badshahronak is offline Offline
Newbie Poster

check your table

 
-1
  #8
16 Days Ago
[plz check your database table you must be inserting value of data type different as in your table for example my table contain username as data type text and I am entering number as the value of username it shows the data type mismatch
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: vernondris is an unknown quantity at this point 
Solved Threads: 0
vernondris vernondris is offline Offline
Newbie Poster
 
0
  #9
5 Days Ago
I'm gonna check it first before anything else,,
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC