469,304 Members | 1,888 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

C# and saving data in access database!

Hi to all ! I have problem with C# and access databasses!

I can't to insert new record in database , ( I mean I made grid view and connect him with a access database and made all things to insert data in column in datagridview and it showing that row is filed with new data but when I restart form in database there is no new record)..

I use for exmpl: same code but with OLEDB;

Expand|Select|Wrap|Line Numbers
  1.  
  2. string connectionString = "Data Source=SERV\\SQLEXP;Initial Catalog=tempdb;Integrated Security=True";
  3. string command = "UPDATE  telefon 
  4. SET prezime="+textBox2.Text+",telefon="+textBox3.Text+" 
  5. WHERE ime ="+textBox1.Text+"";
  6.  
  7. SqlConnection con = new SqlConnection(connectionString);
  8. SqlCommand cmd = new SqlCommand(command, con);
  9.  
  10. con.Open();
  11. cmd.ExecuteNonQuery();
  12. con.Close();
  13. this.telefonTableAdapter.Fill(this.tempdbDataSet.telefon);
  14.  
and this work in SQL but when I use this same code EXCEPT USING SYSTEM.DATA.OLEDB;

it all works well on form it show adding a data to gridview but it not store in database, I was use :
Expand|Select|Wrap|Line Numbers
  1.  this.telefonTableAdapter.Update();
Expand|Select|Wrap|Line Numbers
  1.  this.telefonTableAdapter.Insert();
  2. AcceptChanges();     
but it NOT SAVE DATA TO ACCESS DATABASSES

PLEASE HELP!!!
Feb 15 '07 #1
4 12637
MMcCarthy
14,534 Expert Mod 8TB
Try changing this code ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. string command = "UPDATE  telefon 
  3. SET prezime="+textBox2.Text+",telefon="+textBox3.Text+" 
  4. WHERE ime ="+textBox1.Text+"";
to this ...

Expand|Select|Wrap|Line Numbers
  1.  
  2. string command = "UPDATE  telefon 
  3. SET prezime='" & textBox2.Text & "', telefon='" & textBox3.Text & "'
  4. WHERE ime ='" & textBox1.Text & "'";
  5.  
The single quotes are because Access requires single quotes for text inserts. The ampersand becuase that is what would normally be used but if you feel C# syntax requires the + sign instead then put it back.
Feb 16 '07 #2
thanx! for help I will tray it !
Feb 16 '07 #3
Tnx mmccarthy that is fine now , but ????????


I have this code and it's work on gridview it showing inserted values but it do not save changes in database !!!!

countRow++;
string str;
con.Open();
str = "insert into tele(ime,broj) values('"+textBox1.Text+ "','"+textBox2.Text+ "')";

DataRow dRow;
DataTable dTable = db1DataSet.tele;
dRow = dTable.NewRow();
dRow[1] = textBox1.Text;

if(textBox2.Text!="")
dRow[2] = textBox2.Text;
dTable.Rows.Add(dRow);

DataTable tab1 = db1DataSet.tele.GetChanges();

ole.Update(tab1);
db1DataSet.tele.AcceptChanges();



PLEASE HELP I WAS TRYING A LOT OF THINGS AND EVEN NOW I CAN' T TO SAVE DATA IN ACCESS DATABASE!!
Feb 18 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
string str;
con.Open();
str = "insert into tele(ime,broj) values('"+textBox1.Text+ "','"+textBox2.Text+ "')";
I can only help with the Access side of things. However, one this seems clear. You have put the INSERT statement into a string but you never execute it. Depending on how you connected to the database this would require something like.

Expand|Select|Wrap|Line Numbers
  1. con.execute str
Mayr
Feb 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by manning_news | last post: by
5 posts views Thread by Mark | last post: by
3 posts views Thread by Nathan Guill | last post: by
1 post views Thread by nmrpa91290 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.