Skip to content
Loading
  • 0
    it gives error on these two lines
     
    Sa.Student_id[i] = student[i].Id
    Sa.Course_Id = course[j].Id;
    0
  • Salman Beg
    0
    As you are saying this line gives error,
    Sa.Student_id[i] = Convert.ToInt32(SAC.Student_id[i].ToString());
    means first check the right-hand side which is SAC.Student_id[i].ToString()
    whether it contains any value or not. In the debug mode you can use QuickWatch to know what is the exact value.
    Also, can you try like this,
    Sa.Student_id[i]=s;
    thanks.
    0
  • 0
    Object reference not set to an instance of an object. SAC.Student_id[i].ToString() this line gives error
    0
  • 0
    salman see this image
     
    0
  • Salman Beg
    0
    As this below line is working,
    Sa.Student_id[i]=s;
    So comment out this below line,
    Sa.Student_id[i] = Convert.ToInt32(SAC.Student_id[i].ToString());
    it will work fine then.
    0
  • Ashutosh Gupta
    0
    Mark, 
     
    i see the actual problem now..
     
    you need to initialize your  int[] Student_id and int[] Course_Id. 
     
    create constructor of  your class with size of these array.  e.g.  
    1. public Student_Assigned_courses(int courselen, int srudentlen)  
    2.         {  
    3.             Course_Id = new int[courselen];  
    4.             Student_id = new int[srudentlen];  
    5.         }  
     and when you call this 
    Student_Assigned_courses Sa = new Student_Assigned_courses(SAC.Course_Id.Lenght, SAC.Student_id.Length); 
    it will work now..
    0
  • 0
    Sa.Student_id[i]=s; this gives error of object reference is not set to an instance of object
    0
  • 0
    Ashutosh Gupta after adding constructor it have the values now , but on saving it is not saving into the database the student id and course id are saved as null can you check the attachment in which i explain everything with screensthot
     
     
     
     
    0
  • Ashutosh Gupta
    0
    mark, why do you want to have array of studentid and course id ? anyways you will be saving each student and course as new row. e.g. 2 student with 3 courses will have 6 new rows in database. 
     
    just make it simple schema.
    1. [Table("Student_courses")]  
    2. public class Student_Assigned_courses  
    3. {  
    4. [Key]  
    5. [Display(Name = " ID")]  
    6. public int Id { get; set; }  
    7. [Display(Name = "student Name ")]  
    8. public int Student_id { get; set; }  
    9. [Display(Name = "Course ")]  
    10. [Required(ErrorMessage = "course is Required")]  
    11. public int Course_Id { get; set; }  
    12. [Display(Name = "Module Name")]  
    13. [Display(Name = "Program Name")]  
    14. [Required(ErrorMessage = "Program selection is required field!")]  
    15. public int Program_Id { get; set; }  
    16. [Display(Name = "Year")]  
    17. [Required(ErrorMessage = "Year selection is required field!")]  
    18. public int Year_Id { get; set; }  
    19. }  
     
    1. int i = o  
    2. foreach(var student in SAC.Student_id)  
    3. {  
    4. int j = 0;  
    5. foreach(var course in SAC.Course_Id)  
    6. {  
    7. Student_Assigned_courses Sa = new Student_Assigned_courses();  
    8. Sa.Student_id = Convert.ToInt32(student [i].ToString());  
    9. Sa.Course_Id = Convert.ToInt32(course[j].toString());  
    10. Sa.Module_Id = SAC.Module_Id;  
    11. Sa.Year_Id = SAC.Year_Id;  
    12. Sa.Program_Id = SAC.Program_Id;  
    13. db.StudentCoursesAssigned.Add(Sa);  
    14. j++;  
    15. }  
    16. i++;  
    17. }  
    0
  • 0
    Ashutosh Gupta you are right , but i have multi select dropdown list on view and i want to give an option to the user to select multiple courses and multiple students at the same time , I mean if i select two students and 3 courses then in db it will save 6 rows , 3 courses for student A and 3 courses for student B
    0
  • Ashutosh Gupta
    0
    right, hence do not keep array in DB because anyways you are creating 6 entries in database.
     
    when you load from DB to display in view, just group that data using Linq and send to view. 
    0
  • 0
    Ashutosh Gupta I get struck in saving the record to the db below is my controller code 
    [HttpPost]
    // [ValidateAntiForgeryToken]
    public ActionResult Create(Student_Assigned_courses SAC,FormCollection frmcollection)
    {
    ViewBag.PopulatePrograms = _IEducation.PopulatePrograms();
    if (SAC.Student_id != null)
    {
    foreach(var student in SAC.Student_id)
    {
    foreach(var course in SAC.Course_Id)
    {
    Student_Assigned_courses Sa = new Student_Assigned_courses();
    for(int i=0;i
    {
    int s = SAC.Student_id[i];
    // SAC.Student_id[i] = s;
    Sa.Student_id[i] = s;
    string[] selitems = frmcollection.GetValues("Student_id");
    Sa.Course_Id = SAC.Course_Id;
    Sa.Module_Id = SAC.Module_Id;
    Sa.Year_Id = SAC.Year_Id;
    Sa.Program_Id = SAC.Program_Id;
    db.StudentCoursesAssigned.Add(Sa);
    // Sa.Student_id = SAC.Student_id[i];
    }
    if (db.SaveChanges() > 0)
    {
    TempData["MessageDesignation"] = "Department Saved Successfully!";
    }
    }
    }
    return RedirectToAction("Index");
    }
    return View(SAC);
    }
    in inspect the values and they are there while debugging  
    0
  • Ashutosh Gupta
    0
    1. int i = o    
    2. foreach(var student in SAC.Student_id)    
    3. {    
    4. int j = 0;    
    5. foreach(var course in SAC.Course_Id)    
    6. {    
    7. Student_Assigned_courses Sa = new Student_Assigned_courses();    
    8. Sa.Student_id = Convert.ToInt32(student [i].ToString());    
    9. Sa.Course_Id = Convert.ToInt32(course[j].toString());    
    10. Sa.Module_Id = SAC.Module_Id;    
    11. Sa.Year_Id = SAC.Year_Id;    
    12. Sa.Program_Id = SAC.Program_Id;    
    13. db.StudentCoursesAssigned.Add(Sa);    
    14. j++;    
    15. }    
    16. i++;    
    17. }    
    did you try with my code, it seems your code has extra loop , check values during debugging time, if values does not match with data type, then DB save operation can fail. 
    0
  • 0
    Ashutosh Gupta
    as my student_Id and course_ID properties of type int array in model class
    0
  • 0
    still it save null for student and course id
    0