Sinaesthetic
Sinaesthetic

Reputation: 12211

c# linq: how to submit object to database as a new record

I have the following object:

namespace LearnLINQ1
{
    [Table(Name="testMe")]
    public class SubmitTest
    {
        [Column(Name="FirstName")]
        public string FirstName { get; set; }

        [Column(Name = "LastName")]
        public string LastName { get; set; }

        [Column(Name = "PhoneNumber")]
        public int PhoneNumber { get; set; }

        linqLayerDataContext db;
    }
}

And i've used the same thing with a constructor:

namespace LearnLINQ1
{
    [Table(Name="testMe")]
    public class SubmitTest
    {
        [Column(Name="FirstName")]
        public string FirstName { get; set; }

        [Column(Name = "LastName")]
        public string LastName { get; set; }

        [Column(Name = "PhoneNumber")]
        public int PhoneNumber { get; set; }

        linqLayerDataContext db;

        //constructor
        public SubmitTest(string first, string last, int phone, linqLayerDataContext db)
        {
            this.FirstName = first;
            this.LastName = last;
            this.PhoneNumber = phone;
            this.db = db;
        }

    }
}

I'm using the following code to instantiate the object and attempt to add it to the database as a new record:

SubmitTest test = new SubmitTest { FirstName = "Jeremy", LastName = "Stafford", PhoneNumber = 23 };
db.testMes.InsertOnSubmit(test);

But im getting the error:

Error 1 The best overloaded method match for 'System.Data.Linq.Table.InsertOnSubmit(LearnLINQ1.testMe)' has some invalid arguments C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\LearnLINQ1\LearnLINQ1\Form1.cs 42 13 LearnLINQ1

Error 2 Argument 1: cannot convert from 'LearnLINQ1.SubmitTest' to 'LearnLINQ1.testMe' C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\LearnLINQ1\LearnLINQ1\Form1.cs 42 39 LearnLINQ1

I'm not sure if there was something im missing in the class definition... kinda new to the concept. Can someone point me in the right direction?

================================ UPDATE: Ok so i made some changes

the customer class:

namespace LearnLINQ1
{

    public class Customer
    {
        [Table(Name = "testMe")]
        public class SubmitTest
        {
            [Column(Name = "FirstName")]
            public string FirstName { get; set; }

            [Column(Name = "LastName")]
            public string LastName { get; set; }

            [Column(Name = "PhoneNumber")]
            public int PhoneNumber { get; set; }
        }
    }
}

And the test code:

namespace LearnLINQ1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            linqTestDataContext db = new linqTestDataContext();

            Table<Customer> Customers = db.GetTable<Customer>();
            var cus = new Customer { FirstName = "Jeremy", Lastname = "Stafford", Age = 31 };
            db.Customers.InsertOnSubmit(cus);

            db.SubmitChanges();
        }
    }
}

Now the problem is with the customer class. it is giving the following error:

Error 1 Missing partial modifier on declaration of type 'LearnLINQ1.Customer'; another partial declaration of this type exists C:\Users\Jeremy\Documents\Visual Studio 2010\Projects\LearnLINQ1\LearnLINQ1\Customer.cs 11 18 LearnLINQ1

Upvotes: 1

Views: 934

Answers (1)

Joe
Joe

Reputation: 82624

LearnLINQ1.testMe is the object you need to create not LearnLINQ1.SubmitTest

Upvotes: 1

Related Questions