Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi all
I work with oledb connection for reading and adding from Access DB.
in my form you should fill some text boxes.After that by clicking on AddButton you can add data to DB.
my dataGridView shows new records but when open Access BD from Access no field has been added. it is shocking ....
what is the problem?
OleDbCommand cmd;
                string cmdString = "INSERT INTO  Contacts (FirstName,LastName,Phone,Mobile,Address) values('" + textBoxFirstName.Text + "','" + textBoxLastName.Text + "','" + textBoxPhone.Text + "','" + textBoxMobile.Text + "','" + textBoxAddress.Text + "')";
 
                OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
                cmd = new OleDbCommand(cmdString, myConnection);
                myConnection.Open();
                cmd.ExecuteNonQuery();
                OleDbCommand myCommand = new OleDbCommand();
                myCommand.CommandText = "Select * from Contacts";
                myCommand.CommandType = CommandType.Text;
                myCommand.Connection = myConnection;
 
                OleDbDataAdapter myAdapter = new OleDbDataAdapter();
                myAdapter.SelectCommand = myCommand;
                myAdapter.Fill(myTable);
                dataGridView.DataSource = null;
                dataGridView.DataSource = myTable.DefaultView;
 
                myAdapter.Dispose();
                myCommand.Dispose();
                myConnection.Close();
Posted 25-Nov-12 22:35pm
Edited 25-Nov-12 22:37pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Probably, you are looking at different databases.
Check it:
Open the db using your program, and look at the row available. Close your program.
Open the file in Access, and compare. Are they the same?
If they are, add a row in Access, and close it.
Open the db in your program. Is the new row there?
If so, then you are using the same base file, so it is probably being copied to a different folder as part of the build process.
 

Check the file path in Access, and the exact connection string being read from ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString I think they will be different!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

OleDbCommand cmd;
string cmdString = "INSERT INTO Contacts (FirstName,LastName,Phone,Mobile,Address) values('" + textBoxFirstName.Text + "','" + textBoxLastName.Text + "','" + textBoxPhone.Text + "','" + textBoxMobile.Text + "','" + textBoxAddress.Text + "')";

OleDbConnection myConnection = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

cmd = new OleDbCommand(cmdString, myConnection);
myConnection.Open();
cmd.ExecuteNonQuery();
 
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM CONTACTS",myConnection);
 
System.Data.DataTable myTable= new DataTable(); // Add to u r code

da.fill(myTable);
dataGridView.DataSource = null;
dataGridView.DataSource = myTable.DefaultView;
 
myAdapter.Dispose();
myCommand.Dispose();
myConnection.Close();
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 240
2 DamithSL 200
3 Raul Iloc 185
4 Richard MacCutchan 175
0 OriginalGriff 5,130
1 DamithSL 4,197
2 Maciej Los 3,670
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,821


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 26 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100