Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends
I have an error on my code
I searched in google but I could not solve it.
my code in mvc asp:
C#
if (ModelState.IsValid)
{
   var y = new Student();

   if (t == null)
   {
      // -------
      var Date = Shamsi2Miladi(student.BirthDate.ToString());

      IEnumerable<Student> ww = _repository.Students;
      List<Student> newStudent =
         ww.Select(
            a => new Student() // error line
            {
               Name = a.Name,
               Password = a.Password,
               Education = a.Education,
               Gender = a.Gender,
               CityId = a.CityId,
               About = a.About,
               BirthDate = Date,
               EmailAddress = a.EmailAddress,
               IsActive = a.IsActive,
               StudentID = a.StudentID,
               RecordDate = a.RecordDate,
            })
      .Tolist();

      y=_repository.Students.Add(newStudent); // error line

      _repository.SaveChanges();
   }

   return Json(new { Result = "OK", Record = y });
}

//---------------------------------------
my errors:
1- The best overloaded method match for 'System.Data.Entity.DbSet<myprojectjtable.models.student>.Add(MyProjectJTable.Models.Student)' has some invalid arguments

2- cannot convert from 'System.Collections.Generic.List<myprojectjtable.models.student>' to 'MyProjectJTable.Models.Student'

3- Error 4 'MyProjectJTable.Models.Student' does not contain a constructor that takes 0 parameters

thank you from your answers.
Posted
Updated 8-Mar-15 3:02am
v3

The error message are pretty explicit:
'MyProjectJTable.Models.Student' does not contain a constructor that takes 0 parameters

Which means you can't do this:
C#
a => new Student()
You need to look at your Student class and find out which constructors are available, and call one of those with the appropriate parameters.
We can't do that for you: we can't see your screen, access your HDD, or read your mind!

The other errors are similar problems: there is no conversion from a List<Student> to your model class - so look and see what there is in your model code because we can't!
 
Share this answer
 
Comments
mahmoodof 8-Mar-15 9:27am    
hello
thankyou your answer
i am sorry for delay/.
my student class is very simple and have 1 constructor that is like this:
puplic student(list<student> newStudent)
{
password = "123";
}
so i added another constructor to my class.

puplic student()
{
}
after that i am getting this error :
con not convert ..generic.List<xx> to 'xx'

i have known why this error is comming :
beause of line y=_repository.Students.Add(newStudent);
newStudent should be an entity but that is a list.
(as you seen in my code newStudent is a list of entity not an single entity)

is it right?
if no why?
...
i changed code like this:

student newStudent = _db.student.select(
.
.
.
});
but still it is comming an error.
can not impicity convert type (...ienumerable<xx> to xx) an explicit conversion exists.
1: newStudent is a List<Student> but System.Data.Entity.DbSet.Add(..) (which you're calling with repository.Students.Add(newStudent)) takes a single Student, not a List. To add a List of Students, call AddRange instead of Add.

When you fix that, the next problem will be that y is of Type Student but AddRange will return an IEnumerable<Student>. Also your initialization var y = new Student(); at the top is superfluous (because you never use that single Student instance), aside from not working because of error 3. Remove that line at the top and write instead:
C#
IEnumerable<Student> y = _repository.Students.AddRange(newStudent);


edit after comments: As you're working with EF4, there's no AddRange(..) method. The workaround would be:
C#
foreach(Student student in newStudent)
{
    _repository.Students.Add(student);
}


2: Error 2 should vanish when you fix error 1.

3: Student doesn't have a parameterless constructor. So you can't create an instance of Student like this:
C#
var xyz = new Student();

//and also not like this:
var xyz = new Student() { Name = name, Password = pwd, /*etc*/ };
Look at the definition of the Student class and look there for the signature of the public constructor(s). There might be a constructor like Student(string name) or maybe all of the fields that you initialize in the ww.Select.. are required. So you have to write them like this:
C#
var xyz = new Student(name, pwd, /*etc*/);
 
Share this answer
 
v3
Comments
mahmoodof 8-Mar-15 9:31am    
hello
but i have Addrange Methode in my list.
is it beause of Ef 4?
mahmoodof 8-Mar-15 9:39am    
i am so sorry.
i have No addRange Method on my intelligence list.
how can i do?
[no name] 8-Mar-15 10:13am    
Hello - I updated the solution, please take a look and try that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900