使用循环将数据存储到表中

时间:2014-05-07 22:11:13

标签: sql vb.net sql-server-2008

这是一个向用户提出多项选择问题的项目。

共有5个表:

  1. 表格测验以存储测验ID
  2. 表问题哪个商店问题和id
  3. 表答案存储答案列表
  4. 存储id用户和测验的表结果
  5. 表result_detail,用于存储结果ID和用户回答
  6. 我设法加载问题和答案。我也能够将数据存储到结果表中,但是我无法存储用户的答案。我想要的是当用户选择单选按钮时,答案的ID传递给result_detail表

    以下是代码:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If RadioButton1.Checked = True Or RadioButton2.Checked = True Or RadioButton3.Checked = True Or RadioButton4.Checked = True Then
            If Nombor.Text = 10 Then
                Response.Redirect("EndQues.aspx")
            Else
                Nombor.Text = Val(Nombor.Text) + 1
                Counter += 1
                question()
                clean()
            End If
    
        Else
    
            MsgBox("Error", MsgBoxStyle.Exclamation, "Error")
        End If
    
    End Sub
    
    Sub question()
    conn.Open()
    
        Dim cmd As New SqlCommand("Select * From view_question Where QuestionID=@IdSoalan", conn)
        cmd.Parameters.AddWithValue("@IdSoalan", Counter)
        Dim dr1 As SqlDataReader
        dr1 = cmd.ExecuteReader
    
            If dr1.Read() Then
            Me.lblquest.Text = dr1("QuestionTxt")
            Me.RadioButton1.Text = dr1("1")
            Me.RadioButton2.Text = dr1("2")
            Me.RadioButton3.Text = dr1("3")
            Me.RadioButton4.Text = dr1("4")
    
    
        Else
            conn.Close()
            Counter += 1
            question
        End If
        conn.Close()
    End Sub
    
    
    Sub clean()
        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton3.Checked = False
        RadioButton4.Checked = False
    End Sub
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        question()
        conn.Open()
        Dim cmd1 As New SqlCommand("Insert into TblResult (Result_Quiz_Id,Result_NoMatric,) values (1,'" & Session("NoMatrik") & "')", conn)
        cmd1.ExecuteNonQuery()
        conn.Close()
    End Sub
    

1 个答案:

答案 0 :(得分:0)

我在这里理解的是,您希望选定的单选按钮具有与答案相当的ID吗?我的解决方案是每次添加radiobutton文本时,将ID存储到4个元素的数组中。

Me.RadioButton1.Text = dr1("1")
myArr(0) = dr1("id_1")
Me.RadioButton2.Text = dr1("2")
myArr(1) = dr1("id_2")
Me.RadioButton3.Text = dr1("3")
myArr(2) = dr1("id_3")
Me.RadioButton4.Text = dr1("4")
myArr(3) = dr1("id_4")

然后每次你保存它,只需设置条件来识别所选的单选按钮。

If Me.RadioButton1.Checked = True Then
     answerID = myArr(0)

等等。还有另一种方法,但如果这是您需要的过程,请首先尝试此方法。