再次好日子,我无法弄清楚为什么我的数据在错误的数据字段中保存。 这是它应该如何工作: 完成保存要在节中分配的数据后,它应该能够将其正确保存到指定的Access数据库中。 tblAdviser有三个字段:AdviserID,FacultyID和SectionID。当您创建一个部分时,它会询问您希望哪个部门处理该部分。关于该部分的所有内容都在指定的tblSection中正确保存,但它正在搞乱tblAdviser(它跟踪哪个Faculty是哪个部门的顾问。)
我已经为它创建了一个ID生成器,它可以工作,但是当我完成它之后保存该部分时,而不是Adviser ID生成器将分配的ID保存到tblAdviser中的AdviserID字段。它将FacultyID保存到AdviserID上,当它应该保存在tblAdviser表中的FacultyID数据字段下时。
我应该只在tblSection中包含AdviserID吗?我尝试了但是我还没想出如果他已经有一个在他的翼下,如何不允许该程序使用另一个教师作为顾问。或者我的INSERT语句有问题吗?
让我知道什么是令人困惑的,我会尽量让它更清楚。
Private Sub AddSection(ByVal sSectionID As String, ByVal sSectionTitle As String, ByVal sYearLvl As String, ByVal sCurriculum As String, ByVal sAdviserID As String, ByVal sMaxStudent As String, _
ByVal sCourse As String, ByVal sMajor As String)
If SectionExistByTitle(sSectionTitle) = True Then
MessageBox.Show("Same record found...", "Duplicate Record", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim com As New OleDbCommand()
com.CommandText = "Insert Into [Section](SectionID,SectionName,YearLvl,DepartmentID,MaxStudent,Course,Major) VALUES ('" & sSectionID & "','" & sSectionTitle & "','" & sYearLvl & "','" & sCurriculum & "','" & sMaxStudent & "','" & sCourse & "','" & sMajor & "')"
com.Connection = clsCon.con
com.ExecuteNonQuery()
Dim com1 As New OleDbCommand()
com1.CommandText = "Insert Into [tblAdviser](AdviserID,FacultyID,SectionID) VALUES ('" & sAdviserID & "','" & txtFacultyID.Text & "','" & sSectionID & "')"
com1.Connection = clsCon.con
com1.ExecuteNonQuery()
Dim pipz As New OleDbCommand("Update tblGenerator Set NextNo='" & myID & "' where TableName ='" & "Section" & "'", clsCon.con)
pipz.ExecuteNonQuery()
MessageBox.Show("Record Successfully Saved...")
Me.Close()
End If
End Sub
Public Sub GenerateSectionID()
Dim cmddr As New OleDbCommand("select NextNo from tblGenerator where TableName='" & "Section" & "'", clsCon.con)
Dim dr As OleDbDataReader = cmddr.ExecuteReader()
While dr.Read()
Dim strid As String = dr("NextNo").ToString()
If strid = "" Then
txtSectionID.Text = "SEC-" & "1"
myID = 1
Else
myID = Convert.ToInt32(dr("NextNo")) + 1
txtSectionID.Text = "SEC-" & myID.ToString()
End If
End While
dr.Close()
cmddr.Dispose()
End Sub
Public Sub GenerateAdviserID()
Dim cmddr As New OleDbCommand("select NextNo from tblGenerator where TableName='" & "tblAdviser" & "'", clsCon.con)
Dim dr As OleDbDataReader = cmddr.ExecuteReader()
While dr.Read()
Dim strid As String = dr("NextNo").ToString()
If strid = "" Then
sAdviserID = "ADV-" & "1"
myID = 1
Else
myID = Convert.ToInt32(dr("NextNo")) + 1
sAdviserID = "ADV-" & myID.ToString()
End If
End While
dr.Close()
cmddr.Dispose()
End Sub