Imports System.Data Imports System.Data.SqlClient Partial Class Test2 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblDate.Text = DateTime.Now.ToString("D")
If Not IsPostBack() = True Then
lblError.Text = ""
Session("id_test_int") = 1
Load_data()
If Session("xUser_ID") Is Nothing Then
Response.Redirect("login.aspx")
End If
'รับค่า'
Dim XUser As String
XUser = Session("xUser_ID")
'เลือกฐานข้อมูล'
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=****;User ID=sa;Password=****"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
Dim strSQLcmd As String = " SELECT * FROM member " & _
" WHERE User_ID ='" & Session("xUser_ID") & "'"
Dim Cmd As New SqlCommand(strSQLcmd, objConn)
Dim objDR As SqlDataReader
objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
While objDR.Read()
lblName.Text = objDR("User_ID")
End While
End If
End Sub Sub Load_data()
Session("id_test") = PadL(Session("id_test_int"), 3, "0")
lblId_test.Text = Session("id_test_int")
Dim dvSql As DataView = _ DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
For Each drvSql As DataRowView In dvSql
lblId.Text = drvSql("Id_test")
lblQuestion.Text = drvSql("Question")
lblAnswer.Text = drvSql("Answer")
RadioButton1.Text = drvSql("Choice1")
RadioButton2.Text = drvSql("Choice2")
RadioButton3.Text = drvSql("Choice3")
RadioButton4.Text = drvSql("Choice4")
Next
End Sub Protected Sub RadioButtonTest_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
lblSelect.Text = RadioButton1.Text
ElseIf RadioButton2.Checked = True Then
lblSelect.Text = RadioButton2.Text
ElseIf RadioButton3.Checked = True Then
lblSelect.Text = RadioButton3.Text
Else
lblSelect.Text = RadioButton4.Text
End If
lblError.Text = ""
End Sub Function PadL(ByVal cExpression As String, ByVal nResultSize As Integer, ByVal cPaddingChar As Char) As String
Return cExpression.PadLeft(nResultSize, cPaddingChar) End Function Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
'เช็คว่าซ้ำหรือเปล่า
'เชื่อมต่อกับ SQL Server
Dim strConn1 As String
strConn1 = "Data Source=.\SQLEXPRESS;Initial Catalog=****;User ID =sa;Password =****"
Dim objConn1 As New SqlConnection(strConn1)
objConn1.Open()
'คิวรี่ข้อมูล
'ค้นหาข้อมูล
Dim strSQL1 As String
strSQL1 = "SELECT * FROM mem_test WHERE User_ID = '" & lblName.Text & lblId.Text & lblAnswer.Text & "'"
Dim objCommand1 As New SqlCommand(strSQL1, objConn1)
Dim objDR As SqlDataReader
objDR = objCommand1.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Dim nRec As Integer = 0
While objDR.Read()
nRec = nRec + 1
lblError.Text = "คุณได้ตอบคำถามข้อนี้แล้ว"
End While
Dim Select_choice As String = lblSelect.Text
Dim Answer As String = lblAnswer.Text
If Select_choice = Answer = True Then
lblScore.Text = 1
Else
lblScore.Text = 0
End If
If nRec > 0 Then
Exit Sub
End If
'ตรวจสอบข้อผิดพลาด'
lblError.Text = ""
If lblSelect.Text = "" Then
lblError.Text = "คุณยังไม่ได้เลือกคำตอบ"
lblSelect.Focus()
Return
End If
'บันทึกข้อมูล
'เชื่อมต่อกับฐานข้อมูล SQL Server
Dim strConn As String
strConn = "Data Source=.\SQLEXPRESS;Initial Catalog=****;User ID =sa;Password =****"
Dim objConn As New SqlConnection(strConn)
objConn.Open()
'คำสั่ง SQL สำหรับการเพื่มข้อมูล
Dim strSQL As String
strSQL = "INSERT INTO mem_test (User_ID,Id_Test,Answer,Select_choice,Score,Date)" & _
"VALUES(@User_ID,@Id_Test,@Answer,@Select_choice,@Score,@Date) "
Dim objCommand As New SqlCommand(strSQL, objConn)
objCommand.Parameters.Add(New SqlParameter("@User_ID", lblName.Text))
objCommand.Parameters.Add(New SqlParameter("@Id_Test", lblId.Text))
objCommand.Parameters.Add(New SqlParameter("@Answer", lblAnswer.Text))
objCommand.Parameters.Add(New SqlParameter("@Select_choice", lblSelect.Text))
objCommand.Parameters.Add(New SqlParameter("@Score", lblScore.Text))
objCommand.Parameters.Add(New SqlParameter("@Date", lblDate.Text))
objCommand.ExecuteNonQuery()
'บันทึกข้อมูลเรียบร้อย ทำการล้างค่าที่หน้าจอ เพื่อรอป้อนข้อมูลใหม่
lblId.Text = ""
lblAnswer.Text = ""
lblSelect.Text = ""
lblScore.Text = ""
End Sub
End Class