public bool isRegistered(int courseId, int userId) { user_course userCourse = MCDB.user_course.FirstOrDefault(x => x.course_id == courseId && x.user_id == userId); if (userCourse != null) { return(true); } else { return(false); } }
public bool RegisterThisCourse(int courseId, int userId) { user_course userCourse = new user_course(); userCourse.course_id = courseId; userCourse.user_id = userId; if (!isRegistered(courseId, userId)) { MCDB.user_course.Add(userCourse); MCDB.SaveChanges(); return(true); } else { return(false); } }
private void AddCourseHistories() { System.IO.StreamReader inFile = new System.IO.StreamReader(textBox1.Text); int lineNum = 0; int numInserted = 0; int numSkipped = 0; string inLine; Regex reg = new Regex(@"[a-zA-Z ]{10}\ \d{1}[1-9 ](\ ([A-Z]{2}\-\d{3}\-\d{2}\ {2}|[A-Z]{3}\-\d{3}\-\d{2}\ )[FS]\d{2}\ \d\.\d{2}\ [ABCDEFSUWXOIR][ABCDEFSUWXOIR+\-Q ]{2})+"); bool keepGoing = true; while (!inFile.EndOfStream && keepGoing) { inLine = inFile.ReadLine(); lineNum++; if (!reg.IsMatch(inLine) || (inLine.Length - 13) % 24 != 0) { DialogResult result = MessageBox.Show("Error - inproper input format on line " + lineNum.ToString() + ". Continue?", "Error", MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.No) { keepGoing = false; } } else { linqtoregdbDataContext db = new linqtoregdbDataContext(); string usernameIn, courseNameIn, semesterIn, gradeIn; decimal courseCreditIn; int numCourses; usernameIn = TrimSpaces(inLine.Substring(0, 10)); numCourses = Convert.ToInt32(TrimSpaces(inLine.Substring(11, 2))); for (int i = 0; i < numCourses; i++) { courseNameIn = TrimSpaces(inLine.Substring(14 + i * 24, 10)); semesterIn = TrimSpaces(inLine.Substring(25 + i * 24, 3)); courseCreditIn = Convert.ToDecimal(inLine.Substring(29 + i * 24, 4)); gradeIn = TrimSpaces(inLine.Substring(34 + i * 24, 3)); user_course newUserCourse = new user_course { username = usernameIn, courseName = courseNameIn, grade = gradeIn, semester = semesterIn, courseCredit = courseCreditIn }; try { db.user_courses.InsertOnSubmit(newUserCourse); db.SubmitChanges(); numInserted++; } catch (System.Data.SqlClient.SqlException e) { if (e.Number == 2627) { numSkipped++; } else { DialogResult result = MessageBox.Show("Error - invalid course history insertion attempted. Skip user and continue?", "Error", MessageBoxButtons.YesNo); if (result == System.Windows.Forms.DialogResult.No) { keepGoing = false; } } } } } } string outMessage = numInserted.ToString() + " course history records inserted successfully."; if (numSkipped > 0) { outMessage += " " + numSkipped + " existing records skipped."; } MessageBox.Show(outMessage); inFile.Close(); }
private void button3_Click(object sender, EventArgs e) { //var queryCoursesOffered = from crs in db.courses // where crs.courseTitle == comboBox2.Text // select crs; //string line=""; //System.IO.StreamReader database = new System.IO.StreamReader("courses.in"); //while (!database.EndOfStream) //{ // line=database.ReadLine(); // if(line.Contains(label11.Text)) // break; //} //database.Close(); if (!conflicts()) { linqtoregdbDataContext db = new linqtoregdbDataContext(); //database = new System.IO.StreamReader("courses.in"); //int index=0; //int iterator = 0; //List<string> courses = new List<string>(); //while (!database.EndOfStream) //{ // line = database.ReadLine(); // courses.Add(line); // if (line.Contains(label11.Text)) // index=iterator; // iterator++; //} //database.Close(); var addedClass = (from crs in db.courses where crs.courseName == label11.Text && crs.semester == nextTerm select crs).FirstOrDefault(); var queryOldCourses = from usrcs in db.user_courses where usrcs.username == User.username && usrcs.semester == nextTerm select usrcs; List <user_course> oldCourses = new List <user_course>(); foreach (var c in queryOldCourses) { oldCourses.Add(c); } //int seats=System.Convert.ToInt32(courses[index].Substring(43, 3))-1; //string seatsLeft = System.Convert.ToString(seats); //while(seatsLeft.Length<3) // seatsLeft += " "; addedClass.numSeats -= 1; //courses[index] = courses[index].Substring(0, 43) + seatsLeft + courses[index].Substring(46); //System.IO.StreamWriter outDatabase = new System.IO.StreamWriter("courses.in"); //foreach (string s in courses) // outDatabase.WriteLine(s); //outDatabase.Close(); db.SubmitChanges(); convertLine(); //WHAT DO??? //string courseAdded = label11.Text; //for (int i = courseAdded.Length; i < 11; i++) //{ // courseAdded += " "; //} //courseAdded += nextTerm + " "; //courseAdded += label13.Text + " " + "N " + " "; //Now we call the function to add the new course to the file with the old ones. //PushChanges("classList.in", courseAdded); //linqtoregdbDataContext db = new linqtoregdbDataContext(); var queryCourseID = from crs in db.courses where crs.courseName == label11.Text && crs.semester == nextTerm select crs.courseID; int newCourseID = queryCourseID.First(x => true); user_course uc = new user_course { courseID = newCourseID, username = User.username, courseName = label11.Text, semester = "S15", courseCredit = System.Convert.ToDecimal(label13.Text), grade = "N" }; db.user_courses.InsertOnSubmit(uc); try { db.SubmitChanges(); } catch (DuplicateKeyException exc) { } MessageBox.Show("Course Registered Successfully!"); } richTextBox1.Clear(); richTextBox1.ForeColor = Color.Black; comboBox3_SelectedIndexChanged(sender, e); }