Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ASP.net C# LINQ

Hi ,
I wrote following code its working , but i need to write following code in the LINQ and LINQ lamda

See below my complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentDB;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataClassesDataContext db = new DataClassesDataContext();
        Student st = new Student();
        st.StudentID = "1111";
        st.Studentname = "Test1";
        db.Students.InsertOnSubmit(st);

        Response.Write("Working1!!" + "<br>");

        Course co = new Course();
        co.CourseID = "1231";
        co.Course_Name = "Physics1";
        db.Courses.InsertOnSubmit(co);
        Response.Write("Working2!!" + "<br>");
        db.SubmitChanges();
        CourseStudent ct = new CourseStudent();
        ct.CourseID = "1231";
        ct.StudentID = "1111";
        //db.CourseStudents.InsertOnSubmit(ct);
        
        db.ExecuteCommand("Insert into CourseStudent(CourseID,StudentID) values({0},{1})", ct.CourseID, ct.StudentID);
        Response.Write("Working3!!" + "<br>");

Open in new window

Thanks
Avatar of Stephan
Stephan
Flag of Netherlands image

Why did you comment out the following line?

db.CourseStudents.InsertOnSubmit(ct);

Open in new window


And why do you want lamda expressions and where?
Because I don't see anything that should be converted to LINQ expressions.
Avatar of ukerandi

ASKER

becuase its not worked, after i used  this code its worked
db.ExecuteCommand("Insert into CourseStudent(CourseID,StudentID) values({0},{1})", ct.CourseID, ct.StudentID);

Open in new window

I need example how to do above code using Lamda LINQ
There is no lambda query required in here and I don't see anything that can be converted to a lambda query.

lambda queries or LINQ are mostly done when querying through datasources. In this case when you query to the database it is converted to SQL code.

The thing you are doing is just insert, insert and insert, there is no select or something.

And why are doing the following line before the related table?

db.SubmitChanges();

Open in new window


Does it also break when you do enable the line:
db.CourseStudents.InsertOnSubmit(ct);

Open in new window


and move the following line below it:
db.SubmitChanges();

Open in new window


Something like this:

        DataClassesDataContext db = new DataClassesDataContext();
        Student st = new Student();
        st.StudentID = "1111";
        st.Studentname = "Test1";
        db.Students.InsertOnSubmit(st);

        Response.Write("Working1!!" + "<br>");

        Course co = new Course();
        co.CourseID = "1231";
        co.Course_Name = "Physics1";
        db.Courses.InsertOnSubmit(co);
        Response.Write("Working2!!" + "<br>");

        CourseStudent ct = new CourseStudent();
        ct.CourseID = "1231";
        ct.StudentID = "1111";
        db.CourseStudents.InsertOnSubmit(ct);
        Response.Write("Working3!!" + "<br>");

        db.SubmitChanges();

Open in new window

then i'm getting following Error
 Can't perform Create, Update or Delete operations on 'Table(CourseStudent)' because it has no primary key.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Can't perform Create, Update or Delete operations on 'Table(CourseStudent)' because it has no primary key.

Source Error:


Line 27:         ct.CourseID = "12314";
Line 28:         ct.StudentID = "1111";
Line 29:         db.CourseStudents.InsertOnSubmit(ct);


For more explain CoursID and is Foreign key in the Course Table and StudentID is Foreign Key in the Student Table.

Please let me know why i'm getting above error and how to resolve it.
ASKER CERTIFIED SOLUTION
Avatar of Stephan
Stephan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial