CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    [RESOLVED] LINQ-To-SQL - Simple Insert

    I am having a problem getting LINQ to do a simple insert a specific table. I get a compile error that states:
    'System.Data.Linq.Table<TimeSheet.Customer>' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Data.Linq.Table<TimeSheet.Customer>' could be found (are you missing a using directive or an assembly reference?)"
    Code:
    private void AddCustomer()
    {
        try
        {
            Customer customer = new Customer();
            customer.Name = txtName.Text;
            customer.Address1 = txtAddress1.Text;
            customer.Address2 = txtAddress2.Text;
            customer.City = txtCity.Text;
            customer.State = txtState.Text;
            customer.ZipCode = txtZipCode.Text;
            customer.Contact = txtContactName.Text;
            customer.Email = txtEmail.Text;
            customer.PhoneNumber = txtPhoneNumber.Text;
    
            db.Customers.Add(customer); //Compile error is here
    
            db.SubmitChanges();                
    
            lblInformation.Text = "Customer Added successfully";
            lblInformation.Visible = true;
    
            timer1.Enabled = true;
        }
        catch
        {
            lblInformation.Text = "Adding customer failed.";
            lblInformation.Visible = true;
    
            timer1.Enabled = true;
        }
    }
    here is the database table layout:
    Code:
    CREATE TABLE [dbo].[Customers](
    	[CustomerID] [int] IDENTITY(1,1) NOT NULL,
    	[Name] [varchar](255) NOT NULL,
    	[Address1] [varchar](255) NULL,
    	[Address2] [varchar](255) NULL,
    	[City] [varchar](50) NULL,
    	[State] [varchar](100) NULL,
    	[ZipCode] [varchar](50) NULL,
    	[Contact] [varchar](150) NULL,
    	[PhoneNumber] [varchar](15) NULL,
    	[Email] [varchar](50) NULL,
     CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
    (
    	[CustomerID] ASC )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    Any ideas?

  2. #2
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: [RESOLVED] LINQ-To-SQL - Simple Insert

    nevermind, I figured it out.

    For those that want to know, I replaced this:

    Code:
    db.Customers.Add(customer);
    with this:

    Code:
    db.Customers.InsertOnSubmit(customer);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured