0

在组合框中选择问题 ID 后,相关问题应出现在文本框中。我不确定如何让它工作。我在retrieveQuestion() 上收到错误“类型值......无法转换为字符串”。任何帮助表示赞赏,谢谢。

Private Sub cmbQuestion_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbQuestion.SelectedIndexChanged

            txtExistingQuestion.Text = retrieveQuestion() 'Add question, relevant to Question ID, to text box, DO I NEED .ToString?????????????????      
            loaded = True

        End Sub

        Public Function retrieveQuestion() As List(Of Question) 'Retrieves selected question into text box

            Dim typeList As New List(Of Question)
            Dim Str As String = "SELECT Question_ID, Question_Text FROM Question WHERE Question_ID =" & cmbQuestion.SelectedValue
            Try
                Using conn As New SqlClient.SqlConnection(DBConnection)
                    conn.Open()
                    Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                        Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
                            While drResult.Read
                                typeList.Add(New Question(drResult("Question_ID"), drResult("Question_Text")))
                            End While
                        End Using 'Automatically closes connection
                    End Using
                End Using

            Catch ex As Exception

                MsgBox("Question List Exception: " & ex.Message & vbNewLine & Str)

            End Try

            Return typeList

        End Function

    Public Class Question 'defining one club within class
            Public Sub New(ByVal questionID As Integer, ByVal questionText As String)

                mQuestionID = questionID 'm is for member of the class
                mQuestionText = questionText

            End Sub

            Private mQuestionID As String = ""
            Private mQuestionText As String = ""

            Public Property QuestionID() As String
                Get
                    Return mQuestionID
                End Get
                Set(ByVal value As String)
                    mQuestionID = value
                End Set
            End Property

            Public Property QuestionText() As String
                Get
                    Return mQuestionText
                End Get
                Set(ByVal value As String)
                    mQuestionText = value
                End Set
            End Property
        End Class
4

2 回答 2

1

你的问题是这一行:

mQuestionID = questionID

在您的班级中,您已经定义了这个:

Private mQuestionID As String = ""

但是在你的构造函数中,你说questionID应该是一个Integer,像这样:

Public Sub New(ByVal questionID As Integer, ByVal questionText As String)

您需要将类 ( mQuestionID) 中的支持变量更改为Integer,如下所示:

Private mQuestionID As Integer

这也需要更改 的属性语法QuestionID,如下所示:

Public Property QuestionID() As Integer
    Get
        Return mQuestionID
    End Get
    Set(ByVal value As Integer)
        mQuestionID = value
    End Set
End Property
于 2013-07-26T14:30:02.010 回答
1

您的错误是由声明为 a的retrieveQuestion的返回值引起的,List(Of Question)但随后您尝试设置 TextBox 的 text 属性(并且无法自动将 List(Of Question) 转换为字符串)

所以你可以写这样的东西来提取列表中第一个问题的文本

  Dim qList = retrieveQuestion()
  if qList.Count > 0 then
       txtExistingQuestion.Text = qList(0).QuestionText
       loaded = True
  End If

当然,如果您的查询返回零个或仅返回一个问题,则无需返回 a List(Of Question),您可以将retrieveQuestion方法更改为仅返回一个QuestionNothing

Public Function retrieveQuestion() As Question

      Dim questionResult As Question = Nothing
      ......

      Using drResult As SqlClient.SqlDataReader = cmdQuery.ExecuteReader()
            if drResult.Read() then
                questionResult = New Question(drResult("Question_ID"), _ 
                                     drResult("Question_Text")))
            End if
      End Using 
      ....

      return questionResult
End Function


  Dim question = retrieveQuestion()
  if question IsNot Nothing then
       txtExistingQuestion.Text = question.QuestionText
       loaded = True
  End If

但是,所有关于在您的代码中自动发生的字符串和整数转换的注释确实是一个警钟。您应该努力避免这种转换,因为它们会使您的代码变弱并且容易出现神秘错误。在您的项目属性上切换到Option Strinct On并准备好进行大量转换修复。

于 2013-07-26T14:30:12.447 回答