views:

164

answers:

2

I have a linq query to insert data in table. but its not working. I saw some example on internet tried to do like that but doesn't seems to work.

Tablename: login has 3 columns userid, username and password. I set userid as autoincrementing in database. So I have to insert only username and password everytime.Here's my code.

linq_testDataContext db = new linq_testDataContext();
login insert = new login();
            insert.username = userNameString;
            insert.Password = pwdString;
            db.logins.Attach(insert);// tried to use Add but my intellisence is not showing me Add.I saw attach but dosent seems to work.
            db.SubmitChanges();
+1  A: 

Attach() is the wrong method, you need to call InsertOnSubmit() to let Linq-To-Sql generate an insert statement for you. Attach() is for distributed scenarios, where your entity has not been retrieved via the same data-context that is used for submitting changes.

linq_testDataContext db = new linq_testDataContext();
login insert = new login();
            insert.username = userNameString;
            insert.Password = pwdString;
            db.logins.InsertOnSubmit(insert);// tried to use Add but my intellisence is not showing me Add.I saw attach but dosent seems to work.
            db.SubmitChanges();
Johannes Rudolph
@Johannes Thanks for helping but I tried doin it throws an exception saying "no primary key found" but I have userid (autoincrementing) as primary key in my database.
Ani
Is that also set correctly in your dblm mapping?
Johannes Rudolph
I recreated dbml, now its not throwing any error but data is also not inserted into table.
Ani
+1  A: 

have a look on http://www.codeproject.com/KB/linq/LINQToSQLBaseCRUDClass.aspx

    linq_testDataContext db = new linq_testDataContext();
login insert = new login();
            insert.username = userNameString;
            insert.Password = pwdString;
            db.logins. InsertOnSubmit(insert);
            db.SubmitChanges();

If you Attach - It should attach to the particular object Context .But it wont reflect in database .If you want to insert any values try with InsertOnSubmit(object) and do SubmitChanges() to save it in database

anishmarokey
@anish Thanx, its throwing another exception that goes to linq_test.designer.cs saying"Incorrect AutoSync specification for member 'userid'" , before that I change my dbml to set userid as primary key.Thanks
Ani
try to build the dbml and try ..If you build the dbml only it will create the class.Otherwise also it will throw error ...
anishmarokey
@ Anish Yes I just tried re building dbml. and now its does not throw any exception or error but data is also not inserted in tabel.ThanksAniruddh
Ani
have you tried on InsertOnSubmit()
anishmarokey
login insert = new login(); insert.userid = 2; insert.username = userNameString; insert.Password = pwdString; db.logins.InsertOnSubmit(insert); db.SubmitChanges(); label1.Text = "Sucessfull";This is wht I am trying to do. Last line sets label1 to sucssfull. So when code is sucessfull text prperty of label1 changes to "secssfull" but data not inserted in table
Ani