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
LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StephanLead Software EngineerCommented:
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.
0
ukerandiAuthor Commented:
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
0
StephanLead Software EngineerCommented:
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

0
ukerandiAuthor Commented:
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.
0
StephanLead Software EngineerCommented:
Good information.

Entity Framework and Linq To Sql requires a primary key as identifier for the record. Thats why your insert isn't working.

You have to take a look at your database model.

For example: a student can only have 1 of thesame course?
e.g.:

12314 - 1111
12314 - 1112

But not

12314 - 1111
12314 - 1111

Then you can change CourseId and StudentId both as primary key and keep the foreign key constraint.
Else you need to give it its own identifier and set that one as primary key.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.