从表中读取数据时,我的一个数值返回-1,而不是用户给出的值。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
conn.Open()
Dim command As New MySqlCommand("SELECT * FROM students;", conn)
Dim dataSet As New DataSet()
Dim dataAdapter As New MySqlDataAdapter()
dataAdapter.SelectCommand = command
dataAdapter.Fill(dataSet, "students")
Dim dataTable As DataTable = dataSet.Tables("students")
For x As Integer = 0 To dataTable.Rows.Count - 1
Dim newStudent As New Student
newStudent.intIDNum = dataTable.Rows(x).Item("idNumber")
newStudent.strFirstName = dataTable.Rows(x).Item("firstName")
newStudent.strLastName = dataTable.Rows(x).Item("lastName")
newStudent.chrGender = dataTable.Rows(x).Item("gender")
newStudent.dateDOB = dataTable.Rows(x).Item("dateOfBirth")
newStudent.intAge = CInt(Today.Year - newStudent.dateDOB.Year)
newStudent.intYearGroup = dataTable.Rows(x).Item("yearGroup")
newStudent.intSkillLevel = dataTable.Rows(x).Item("skillLevel")
studentList.Add(newStudent)
lstStudents.Items.Add(newStudent.nameConcat(newStudent.strFirstName, newStudent.strLastName))
Next
conn.Close()
Catch ex As Exception
MsgBox("Error: " & ex.ToString())
End Try
End Sub
当读取值" skillLevel"时,它应该在1和4之间读取,但它读取-1。通过MySQL直接添加到数据库的任何数据都没有发生这种情况,但是在我的程序中使用该方法时,似乎出现了这个问题。
Dim newStudent As New Student
newStudent.strFirstName = txtForename.Text
newStudent.strLastName = txtSurname.Text
newStudent.dateDOB = dtpStudentDOB.Value
If rdbMale.Checked = True Then
newStudent.chrGender = "M"
Else
newStudent.chrGender = "F"
End If
newStudent.intAge = newStudent.calcAge(dtpStudentDOB.Value)
newStudent.intSkillLevel = cmbSkillLevel.SelectedItem
newStudent.intYearGroup = cmbYearGroup.SelectedItem
newStudent.intIDNum = CInt(txtStudentID.Text)
Dim conn As New MySqlConnection("server=localhost;user=root;database=new_project;port=3306;password=********;")
Try
conn.Open()
Dim command As MySqlCommand = New MySqlCommand
command.Connection = conn
command.CommandText = "INSERT INTO Students VALUES(@IdNumber, @firstName, @lastName, @gender, @dateOfBirth, @yearGroup, @skillLevel);"
command.Prepare()
command.Parameters.AddWithValue("@IdNumber", newStudent.intIDNum)
command.Parameters.AddWithValue("@firstName", newStudent.strFirstName)
command.Parameters.AddWithValue("@lastName", newStudent.strLastName)
command.Parameters.AddWithValue("@gender", newStudent.chrGender)
command.Parameters.AddWithValue("@dateOfBirth", newStudent.dateDOB)
command.Parameters.AddWithValue("@yearGroup", newStudent.intYearGroup)
command.Parameters.AddWithValue("@skillLevel", newStudent.intSkillLevel)
command.ExecuteNonQuery()
Catch ex As Exception
MsgBox("Error: " & ex.ToString())
Finally
conn.Close()
End Try
这是将学生添加到系统的代码部分。究竟是什么原因导致出现此错误?
作为参考,我在数据中添加的主要表单使用枚举:
Private Enum SkillLevels As Integer
One = 1
Two = 2
Three = 3
Four = 4
End Enum
然后我将其链接到下拉列表:cmbSkillLevel.DataSource = System.Enum.GetValues(GetType(SkillLevels))
答案 0 :(得分:0)
显而易见的可能是在SKILL-LEVEL下拉列表中没有选择,这就是为什么你得到-1,你可以通过确保它不是-1来检查是否在下拉列表中做出选择
例如,你的技能下降可能看起来像这样
<select id="SkillLevel">
<option value="0" selected="selected">Please Select</option>
<option value="1">Low</option>
<option value="2">Medium</option>
<option value="3">High</option>
<option value="4">Very-High</option>
</select>
如果没有选择,返回的默认值将为零(0) 并且您可以通过查找返回值来检查有效值&gt; 0
据我所知,如果省略selected =“selected”,则返回值为-1
答案 1 :(得分:0)
我想我错误地解释了@ tolanj的评论。看来,在加载我的数据时,系统无法读取我的skillLevel列。为了解决这个问题,我将newStudent.intSkillLevel = row.Item("skillLevel")
更改为newStudent.intSkillLevel = SByte.Parse(row.Item("skillLevel"))
。