Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, Using Visual studio 2010, added service based Database in VS 2010... When I run following code I have runs without an error but when I check my database after exiting application, row is not added in database...

but I see new record in datagridview...

I tried following:

Creating another database
creating another application
tried inserting with sqlconnection
tried on another machine.

C#
dbDataContext db = new dbDataContext();

var newitem = new emp_detail();

// newitem.user_id= txteid.Text.Trim();

newitem.com_id = 1;

newitem.f_name= txtefname.Text.Trim();

newitem.l_name= txtelname.Text.Trim();

newitem.address= txteadd.Text.Trim();

newitem.position= txteposition.Text.Trim();

newitem.b_date= dtpebdate.Text.ToString();

newitem.h_date= dtpehdate.Text.ToString();

newitem.notes= txtenotes.Text.Trim();

newitem.d_wages= txtedailywages.Text.Trim();

newitem.h_phone= txtehphone.Text.Trim();

newitem.mob_no= txtemobno.Text.Trim();

newitem.bonus= txtebonus.Text.Trim();

newitem.e_fname= txtecfname.Text.Trim();

newitem.e_lname= txteclname.Text.Trim();

newitem.e_relationship= txtecrelnshp.Text.Trim();

newitem.e_phone= txtecphone.Text.Trim();

newitem.e_mobno= txtecmobno.Text.Trim();

newitem.ot_wages= txteotwages.Text.Trim();

db.emp_details.InsertOnSubmit(newitem);

db.SubmitChanges();

MessageBox.Show("Saved");
Posted
Updated 25-Sep-11 6:08am
v2

You must have a primary key defined on your table for the update to work.
 
Share this answer
 
Comments
Simon Bang Terkildsen 25-Sep-11 15:41pm    
I didn't believe that you're right about LINQ to SQL requiring a primary key, but after a quick google I found your right! But comeon that's just wrong..
my 5
Mehdi Gholam 25-Sep-11 15:44pm    
HeHe! I did the same! strange, yes?! (google is amazing!)
Simon Bang Terkildsen 25-Sep-11 15:48pm    
ups forgot to vote I see, I've done it now :)
Yeah seems like a very strange requirement. I'll have to research why they felt it was necessary, probably some weird design issue.
Mehdi Gholam 25-Sep-11 15:53pm    
I think its creating a transaction then when it can't get back an identity it rolls back.
Simon Bang Terkildsen 25-Sep-11 15:50pm    
Google - the number one dev tool
That should be an entry in every vocabulary :)
Dear fellow,

When you add such databases like service based databases or compact edition databases, upon running the project they are hosted with the solution file every time in the bin folder. so they are no more pointing to the db contained in your solution file. They start to point the one hosted in the bin folder.

Take a deep breath and do the following,

Place the database in some other place and update all the previous references to this one like in app.config and you are done. See, the records will be inserted/updated.

Also do the following corrections in the code,

C#
dbDataContext db = new dbDataContext();
 
var newitem = new emp_detail();
 
// newitem.user_id= txteid.Text.Trim();

newitem.com_id = 1;
 
newitem.f_name= txtefname.Text.Trim();
 
newitem.l_name= txtelname.Text.Trim();
 
newitem.address= txteadd.Text.Trim();
 
newitem.position= txteposition.Text.Trim();
 
newitem.b_date= dtpebdate.Text.ToString();
 
newitem.h_date= dtpehdate.Text.ToString();
 
newitem.notes= txtenotes.Text.Trim();
 
newitem.d_wages= txtedailywages.Text.Trim();
 
newitem.h_phone= txtehphone.Text.Trim();
 
newitem.mob_no= txtemobno.Text.Trim();
 
newitem.bonus= txtebonus.Text.Trim();
 
newitem.e_fname= txtecfname.Text.Trim();
 
newitem.e_lname= txteclname.Text.Trim();
 
newitem.e_relationship= txtecrelnshp.Text.Trim();
 
newitem.e_phone= txtecphone.Text.Trim();
 
newitem.e_mobno= txtecmobno.Text.Trim();
 
newitem.ot_wages= txteotwages.Text.Trim();
 
db.AddToemp_details(newitem); // corrected
 
db.SubmitChanges(); // or db.SaveChanges();  if you are using entity framework
 
MessageBox.Show("Saved");



Good luck!
 
Share this answer
 
v2
Comments
ankithmt 27-Sep-11 11:14am    
If i change the path then it is working perfect but that way I can't make my application's path dynamic ?
ankithmt 27-Sep-11 11:28am    
I'm keeping database just one step up of path... but is there any way to make path dynamic ???
Syed Ammar Haider 27-Sep-11 23:53pm    
Well, to make the application dynamic, you need Sql Server (any edition) except Sql server compact edition. Create a database separately using the Sql Server Management studio, and link it to your application. Here you go!

Please do not forget to mark the post as Answer if you find it useful.

Good Luck!
Hi,

change your object creation line to

C#
Emp_detail newitem = new Emp_detail();


I hope it works for you too

All the Best
 
Share this answer
 
Comments
Syed Ammar Haider 28-Sep-11 3:37am    
Yes, its a good practise to be explicit.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900