LINQ - Insert in LINQ to SQL

Asked By chandu
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
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
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
Create New Account
help
LINQ InsertOnSubmit return type Hi, I am using LINQ to insert data. using (MSDNMagazine. JsonDataContext context = new MSDNMagazine. JsonDataContext ()) { MSDNMagazine. emp employee = new MSDNMagazine. emp () { emp_cod = Convert .ToDecimal(objData.EmpCode.Trim()), emp_nam = objData.EmpName.Trim() }; context.emps.InsertOnSubmit(employee); context.SubmitChanges(); } This code works fine but I need the return type as ExecuteNonQuery you can use Convert.ToInt32 with your return type. Hope this will help you Hi. . InsertOnSubmit() Doesn't return any values. . . If you want to conform whether the operation was successful Dim odb As New DataClassesDataContext Dim tst As New test tst.name = "abcd" odb.tests.InsertOnSubmit(tst) odb.SubmitChanges() Response.Write("id:" + tst.id.ToString) InsertOnSubmit doesn't return any value because the return type of this method is void. Create
IDENTITY_INSERT and linq SQL Server I have been trying to get IDENTITY_INSERT ON to work with linq insertOnSubmit with SQLExpress. It seems SQL is ignoring the IDENTITY_INSERT. Any Ideas? ie. db.ExecuteCommand("SET IDENTITY_INSERT cdTEMPERTest ON"); db.cdTEMPERTests.InsertOnSubmit(newRow); db.SubmitChanges(); SQL Server Programming Discussions SQL Server (1) CdTEMPERTest (1) Stored procedure (1 Exception (1) Class (1) LINQ (1) Database (1) InsertOnSubmit (1) Did you set an explicit value for the identity column? On 10 / 27 / 08 problem is that the SET IDENTITY_INSERT ON is being executed in a different session than InsertOnSubmit(). . . This is one of the advantages of using stored procedures over LINQ. If you are generating your own IDENTITY value in the app, then why are you
LINQ Insert with Unique Identifier field -> automaticly created? I am working with linq to insert a new record. Do i need to specify a new Unique Key to insert the object, it is identified as "uniqueidentifier" in my MS SQL Database. Or how do userName = inShare.username End With Data Acces Layer Public Function insertShare( ByVal shar As Entities.Linq.shareInfo) As Boolean Using dcPacs 'My LINQ DataContext Object If dcPacs.shareInfos.Count( Function (d) d.patientId = shar.patientId AndAlso d.statusPat = shar.statusPat) = 0 Then dcPacs.shareInfos.InsertOnSubmit(shar) dcPacs.SubmitChanges() insertShare = True Else insertShare = False End If End Using End Function on
Linq insert vs stored procedure i m using visual studio 2008 and developing application with LINQ. now i have 2 option to insert record first is to create stored procedure and call that sp through other option is to use 'InsertOnSubmit' method of LINQ. which option is better / right and why pls suggest. . . . . . If you asking according to the they the same if you consider data versions in the SP, difference is apperance. . but Linq has a gol with by its apperance like it is showing in another layer good in any manner desirable without the data consumer being affected. There is a layer separation Linq: 1.There is no control over this in and many real world scenarios there could
LINQ to SQl Hello All, I have to convert the following sql query in to LINQ insert into tblab(a, b) select x, y from tblxy please suggest. . . Thanks and Regards vijay work it out http: / / chiragrdarji.wordpress.com / 2007 / 08 / 25 / how-to-use-transaction-in-linq-using-c / thank you www.codegain.com try this link convert sql query in to LINQ http: / / blogs.msdn.com / vbteam / archive / 2007 / 12 / 04 / converting-sql-to-linq-part-3-distinct-where-order-by-and-operators-bill-horst.aspx please refer following blog how to work with ur needs http: / / weblogs.asp.net / scottgu / archive / 2007 / 08 / 27 / linq-to-sql-part-8-executing-custom-sql-expressions.aspx Hi Vijay, You can write your own linq code like this:- For Insert query:- AB ab = new AB(); (AB is a class corresponding