Click here to Skip to main content
Sign Up to vote bad
good
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 - 21:35
Edited 25 Nov '12 - 21:37


2 solutions

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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 445
1 OriginalGriff 295
2 Arun Vasu 253
3 Zoltán Zörgő 194
4 CPallini 173
0 Sergey Alexandrovich Kryukov 10,105
1 OriginalGriff 7,739
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 2,999


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