Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:38:57+00:00 2026-05-29T19:38:57+00:00

I am trying to insert a record using Link to SQL. I know I

  • 0

I am trying to insert a record using Link to SQL. I know I need to create a new object that includes the column data to be submitted. But How do I identify that object name?

I am successfully able to delete the record, but not able to insert record as I am not able to create a new “student” object

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Student.Models;

namespace Student.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/

        private IStudentRepository  _repository;

        public StudentController() : this(new StudentRepository())
        {
        }

        public StudentController(IStudentRepository repository)
        {
            _repository = repository;
        }

        public ActionResult Index()
        {    
             return View(_repository.ListAll());
        }


        public ActionResult RemoveStudent(int id)
        {

            StudentDataContext student= new StudentDataContext();

            var std = student.Students.Single(h => h.StudentID == id);
            student.Students.DeleteOnSubmit(std);
            student.SubmitChanges();
            return Content("Student " + id + " Removed");
        }

        public ActionResult AddStudent(int studentID)
        {
            StudentDataContext student= new StudentDataContext();

            var std = student.Students.FirstOrDefault(h => h.StudentID == studentID);

            if (std == null || std.StudentID < 1)
            {

            student.Students.InsertOnSubmit(std);
            student.SubmitChanges();
            return Content("Student " + id + " Added");

            }

            return Content("Student  " + studentID + " is already present");
        }

Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:38:58+00:00Added an answer on May 29, 2026 at 7:38 pm

    You need to create a new Student() – or whatever the name of your object is.

    if (std == null || std.StudentID < 1)
    {
        // create a new student before trying to insert
        std = new Student() { StudentID = id };
    
        student.Students.InsertOnSubmit(std);
        student.SubmitChanges();
        return Content("Student " + id + " Added");
    }
    

    From my reading your question is

    But How do I identify that object name?

    i.e. “what is the name of the class?”

    You can see this on the LINQ-to-SQL designer. The name of the class is the same as the name of the object on the designer which describes the table. The default name for the class is the same as the table – but if it clashes with another class of that name, then it will be renamed to Student1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I need help with trying to insert a record using MVC and Entity Framework.
I'm new to databases and am trying to add a new record using SQL.
I am trying to insert a record into a table using Linq but get
I am experiencing a very frustrating issue when trying to insert a new record
I am trying to identify SQL INSERTS that occur when a record is viewed
I am trying to insert and update data using 3 prepared statements. There are
I'm trying to load some data using sql loader. Here is the top of
I am trying to parse XML using PHP DOM and then insert this data
I have a database that has been populated with some data using create and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.