LINQ - Insert in LINQ to SQL - Asked By chandu on 13-Jan-11 11:04 PM

I am inserting records into db using linq to sql

 protected void Button2_Click(object sender, EventArgs e)
    {

      using (DataClassesDataContext dc1 = new DataClassesDataContext())
      {
        Product product = new product();
        product.id = TextBox1.Text;
        product.name = TextBox2.Text;

        dc1.Products.InsertOnSubmit(product);
        dc1.SubmitOnChanges();
    

      }

Here im getting the error like the type or namespace name 'product couldnot be found,and i am not gettting the insertonsubmit with intellisense i dont know why aby help?
Anoop S replied to chandu on 14-Jan-11 03:37 AM
Change the code to

// Create a new Product object.
 
Product product = new product
{
 product.id = TextBox1.Text,
 product.name = TextBox2.Text
};
 
// Add the new object to the Product collection.
dc1.Products.InsertOnSubmit(product);
 
// Submit the change to the database.
try
{
  dc1.SubmitChanges();
}
Daivagna Nanavati replied to chandu on 14-Jan-11 09:49 AM
Hi Chandu

Make sure that the class in which you are trying to access this Product class and Product class itself both have same namespace, otherwise what will happen is you linq class where you are trying to access Product will not able to locate that class so in that case you will need to include name space manually like following, say suppose your Product class is in Products name space so it would be accessible like following

 
using Products;
protected void Button2_Click(object sender, EventArgs e)
{
   using (DataClassesDataContext dc1 = new DataClassesDataContext())
   {
     Product product = new product
     {
       id = TextBox1.Text;
       name = TextBox2.Text;
     }
     dc1.Products.InsertOnSubmit(product);
     dc1.SubmitOnChanges();
    
   }
}

you can see the namespaces included, just check in your code and see if namespace are equal or not, you can also make them equal by removing one of the not required nsmaespace

let me know

Thanks