在ComboBox中选择ID时,难以在TextBox中检索文本

时间:2013-07-26 14:19:35

标签: sql-server vb.net data-binding combobox textbox

在组合框中选择问题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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

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

当然,如果您的查询返回零或只有一个问题,则无需返回List(Of Question),您可以更改 retrieveQuestion 方法,只返回{{1 }或Question

Nothing

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