Input string was not in a correct format. Conversion from string "Normal" to type 'Integer' is not valid

StackOverflow https://stackoverflow.com/questions/23637767

  •  21-07-2023
  •  | 
  •  

Pregunta

Im creating a multiple choice question and user choose the answer from radiobutton. After user chose the answer, it will be stored into array myArr.

How to store the answerId as integer as result in resultdetail table? I run this code and error " Input string was not in a correct format ".

Dim myArr(3) As String
    Dim cmd As New SqlCommand("Select * From view_Question Where QuestionID=@IdQuestion", conn)
    cmd.Parameters.AddWithValue("@IdQuestion", Counter)
    Dim dr1 As SqlDataReader
    dr1 = cmd.ExecuteReader

        If dr1.Read() Then
        Me.lblQuestion.Text = dr1("QuestionTxt")
        Me.RadioButton1.Text = dr1("1")
        myArr(0) = dr1("1")
        Me.RadioButton2.Text = dr1("2")
        myArr(1) = dr1("2")
        Me.RadioButton3.Text = dr1("3")
        myArr(2) = dr1("3")
        Me.RadioButton4.Text = dr1("4")
        myArr(3) = dr1("4")

        Dim answerId As String

        If Me.RadioButton1.Checked = True Then
            answerId = myArr(0)
            Dim answer As Integer = Convert.ToInt32(answerId)
            Session("jaw") = answerId
        ElseIf Me.RadioButton2.Checked = True Then
            answerId = myArr(1)
            Dim answer As Integer = CInt(answerId)
            Session("jaw") = answerId
        ElseIf Me.RadioButton3.Checked = True Then
            answerId = myArr(2)
            Dim answer As Integer = CInt(answerId)
            Session("jaw") = answerId
        ElseIf Me.RadioButton4.Checked = True Then
            answerId = myArr(3)
            Dim answer As Integer = CInt(answerId)
            Session("jaw") = answerId
        End If

        'Dim answer As Integer = CInt(answerId)
        'Session("jaw") = answerId

    Else
        conn.Close()
        Counter += 1
        soalan()
    End If
    conn.Close()
End Sub
¿Fue útil?

Solución

It looks like you are getting the text of the answer back from the datareader; that just doesn't translate to the ID; you have to query the ID as well. Or by answer ID do you mean 1, 2, 3, 4? If that is the case, just hard-code it based on the radio button checked:

 If Me.RadioButton1.Checked = True Then
     answerId = 1 ' since first radio is checked
     Dim answer As Integer = Convert.ToInt32(answerId)
     Session("jaw") = answerId
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top