5 Replies - 11049 Views - Last Post: 25 March 2010 - 09:22 AM Rate Topic: -----

#1 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

C# linq to sql insert problem

Posted 25 March 2010 - 08:13 AM

Hello

I'm starting a new project and decided to use Linq to access the database. It appears to be relatively easy. It didnt take long to get my database output to screen using a GridView. But then I tried to insert new data into the database and failed. I've looked at a number of tutorials and they all say to use "db.members.add(member)". However when I try this the "add" keyword is not appearing for me. I dont know what the problem is. The following is a screen shot of what is happening.

Posted Image

I have also tried using "db.Members.insertonsubmit(member)" followed by "db.submitChanges()". This didnt work either.

The following is the code I've started with. Note: this is just an application I started to practice using to Linq.

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            NisusDataContext db = new NisusDataContext();

            Member member = new Member();
            member.FirstName = "John";
            member.LastName = "Doe";

            

            var getAllTeams = from t in db.Members select t;

            GridView1.DataSource = getAllTeams;
            GridView1.DataBind();
        }
    }
}



Any help will be greatly appreciated.


Regards,



Eamon

Is This A Good Question/Topic? 0
  • +

Replies To: C# linq to sql insert problem

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 380
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# linq to sql insert problem

Posted 25 March 2010 - 08:30 AM

OK, I'm not really a LINQ TO SQL guy, but could you try this code, which works for me:

db.GetTable<Member>().Insertonsubmit(member);
db.SubmitChanges();




Hope this helps.

Also this should work:

db.Members.Insertonsubmit(member);
db.SubmitChanges();


This post has been edited by FlashM: 25 March 2010 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 380
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: C# linq to sql insert problem

Posted 25 March 2010 - 08:42 AM

Do you get any exception thrown when trying to insert new member? How did you configure your data context?

I configured mine like this:

ModelDataContext dc = new ModelDataContext(@"Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=testdb;User Id=test;Password=test;");



You need to provide a connection string for your data context or you need to have your connection string set in app.config file.
Was This Post Helpful? 0
  • +
  • -

#4 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

Re: C# linq to sql insert problem

Posted 25 March 2010 - 08:58 AM

db.GetTable<Member>().Insertonsubmit(member); 
db.SubmitChanges();


The above is getting me an error as follows:

Violation of PRIMARY KEY constraint 'PK_Member'. Cannot insert duplicate key in object 'dbo.Member'.

I thought that linq handles updating the pk automatically? I'm not assigning a duplicate pk, I'm only assingin a first & last name. The Member pk is MemberId which I am not using.

I had already tried
db.Members.Insertonsubmit(member); 
db.SubmitChanges();
but this doesnt yield any errors nor does it update anything.


I assume my data context is configured correctly as I have access to the database. I can retrieve data, I just cant write to it. I didnt do any configuring to it really. I followed a tutorial where I created my Linq to SQL class, then dragged my tables from the server explorer onto the Linq To SQL class design page. The tutorial is: http://weblogs.asp.n...sql-part-1.aspx
He posted a video tutorial which I followed.
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: C# linq to sql insert problem

Posted 25 March 2010 - 09:04 AM

That error is saying that you have a primary key that you're trying to duplicate, which SQL isn't going to allow to happen. What I would suggest is making an auto increment field (IDENTITY) column in your SQL table and make that your primary key
Was This Post Helpful? 2
  • +
  • -

#6 eamon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 25-March 10

Re: C# linq to sql insert problem

Posted 25 March 2010 - 09:22 AM

Thanks PsychoCoder

It was the identity that was the problem. Once I applied it to my database tables I was able to insert new data, altho I still dont have an "add" keyword like in the tutorial but its working so thats the main thing.

Thanks FlashM for your help too. Both snippets of code that you suggested are now working.

Whats going to work...teamwork!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1