Reputation: 35
I ask here because I'm having a problem with my code.
It's suppose to update an SQL Database but instead it shows an error which is
Key cannot be null. Parameter name: key
It highlights the SQLConnection.Open()
Private Sub btnTakeQuiz_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTakeQuiz.Click
Dim SQLStatement As String = "UPDATE class SET exam=Yes WHERE name = " & Session("name") & ""
TakeQuiz(SQLStatement)
End Sub
Public Sub TakeQuiz(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
SQLConnection.Open()
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
SQLConnection.Dispose()
Server.Transfer("Quiz.aspx", True)
End Sub
Session("name") Contains the current login user name.
Class is my table.
exam is a column if it's yes then it means the user has took an exam.
What I'm trying to do is to limit a user to one quiz only. Can anyone help me?
Upvotes: 0
Views: 382
Reputation: 2845
The "Yes" and session value will be treated as a column or data source which will give a query like this.
UPDATE class SET exam=Yes WHERE name = <SessionValue>
Can you try this?
Dim SQLStatement As String = "UPDATE class SET exam='Yes' WHERE name = '" & Session("name") & "'"
Upvotes: 1