1

I'm working on a small Information System Application. I'm making it with windows forms and Service-based Database. It's almost done but when I execute the application a very odd problem appears. I make a stored procedure for inserting data in db tables and when i execute it manually it all works fine, but when I start the program and execute the procedure through button click something wrong is happened. At first sight the program is working fine and the procedure is executing well, but the data is not stored in the db. Here is my code:

UserData.cs

 public static bool AddStudent(Student std)
    {
        UserDataClassesDataContext dc = new UserDataClassesDataContext();
        try
        {
            dc.AddNewStudent(std.FirstName, std.SecondName, std.LastName, std.Faculty, std.Specialty, std.OKS,
                std.StudentStatus, std.FacNumber, std.Course, std.Potok, std.Group);
            dc.SubmitChanges();
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }

StudentValidation.cs

public bool InsertStudent(Student std)
    {
        return UserData.AddStudent(std);
    }

MainForm.cs buttonClick event

private void insertStudentButton_Click(object sender, EventArgs e)
    {
        Student student = new Student();
        student.FirstName = tbFirstName.Text;
        student.SecondName = tbSecondName.Text;
        student.LastName = tbLastName.Text;
        student.Faculty = tbFacultyName.Text;
        student.Specialty = tbSpecialty.Text;
        student.FacNumber = tbFacNumber.Text;
        student.OKS = (short)cbOKS.SelectedIndex;
        student.StudentStatus = (short)cbStudentStatus.SelectedIndex;
        student.Course = (short)numCourse.Value;
        student.Potok = tbFlow.Text;
        student.Group = tbGroup.Text;
        if (sv.InsertStudent(student) && sv.InsertUser(student))
            MessageBox.Show("The student is added successfully!");
        else
            MessageBox.Show("A problem occurs while trying to add the student!");
    }

I think that the db is somehow disconnected when I start my application, but I don't know why.

4

1 回答 1

0

也许你应该试试这个方法:

db.students.InsertOnSubmit(std);

有关更多信息,请参阅此链接 http://msdn.microsoft.com/en-us/library/bb386941.aspx

于 2013-03-08T13:10:04.487 回答