2013-07-26 12 views
0

在組合框中選擇一個問題ID後,相關問題應該出現在文本框中。我不確定如何讓這個工作。在retrieveQuestion()上收到錯誤「類型的值不能轉換爲字符串」。謝謝任何幫助,謝謝。在組合框中選擇ID時,在文本框中檢索文本時很困難

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 
+0

你的錯誤發生在哪一行? – gwin003

+0

你可以發佈'問題'類代碼? –

+0

retrieveQuestion() - 當我嘗試調用QuestionText相對於QuestionID的函數 – Nick

回答

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 
+0

謝謝,我已經注意到了這一點,但是改爲整數提供了進一步的錯誤,一旦表單運行 – Nick

+0

將其更改爲「Integer」會引入什麼錯誤? –

+0

首先,在retrieveQuestion()下仍然有一個錯誤,但現在當我註釋該行時,出現錯誤「問題列表異常:從字符串轉換」「到Integer類型無效」雖然構造函數是一個字符串,它會至少允許組合框工作。 – Nick

1

你的錯誤是由返回值retrieveQuestion聲明爲List(Of Question),但然後嘗試設置文本框的文本屬性(並且沒有辦法自動將List(問題)轉換爲字符串)

所以,你可以寫這樣的事情來提取的第一個問題的文本列表

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

當然,如果您的查詢返回零或只是一個問題,那麼就沒有必要返回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,併爲您準備好大量的轉換修復。

+0

海報的'Question'類對'questionID'參數和'mQuestionID'成員變量的類型不匹配。構造函數期望收到一個'Integer',但是成員變量期望被分配一個'String'; VB.NET不能隱式地將'Integer'轉換爲'String'。 –

+0

現在不能測試,但OPTION STRICT OFF? – Steve

+0

感謝您的幫助,我將代碼更改爲您的建議,但我仍然在retrieveQuestion()上收到同樣的錯誤() – Nick

相關問題