Link to home
Start Free TrialLog in
Avatar of 98fatboyrider
98fatboyrider

asked on

Stuck creating Jeopardy style game

I'm trying to create a Jeopardy style game. The board has six columns with five rows of buttons. When the user clicks a button on the form, it should pull up a form and populates the labels with the question and four possible answers. I've had some assistance creating the datatables earlier. Unfortunately this person is no longer available to help me finish what we've started. Now I'm not sure where he left off or how to continue. Can anyone help me finish what was started? As it is now, I receive the error "Object reference not set to an instance of an object" when clicking on any of the buttons. Any help would be greatly appreciated.
~TIA
Public Class frmBoard
    Inherits System.Windows.Forms.Form
 
    Private m_Categories As DataTable
    Private m_Question As String
 
    Dim dtQuestions As DataTable
    Dim arrCol1(4) As Button
    Dim arrCol2(4) As Button
    Dim arrCol3(4) As Button
    Dim arrCol4(4) As Button
    Dim arrCol5(4) As Button
    Dim arrCol6(4) As Button
 
    Private Sub frmGame_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oBoard As New ClsBoard
        Dim dtCategories As New DataTable
 
        'hook up array
        arrCol1(0) = btn1200
        arrCol1(1) = btn1400
        arrCol1(2) = btn1600
        arrCol1(3) = btn1800
        arrCol1(4) = btn11000
 
        dtCategories = oBoard.GetCategories
        txtCat1.Text = dtCategories.Rows(0).Item(1)
        txtCat2.Text = dtCategories.Rows(1).Item(1)
        txtCat3.Text = dtCategories.Rows(2).Item(1)
        txtCat4.Text = dtCategories.Rows(3).Item(1)
        txtCat5.Text = dtCategories.Rows(4).Item(1)
        txtCat6.Text = dtCategories.Rows(5).Item(1)
        For x = 0 To 4
            arrCol1(x).Text = (x + 1) * 200
            arrCol1(x).Tag = x
        Next
        Me.Show()
    End Sub
 
    Private Sub btnCol1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1200.Click, btn1400.Click, btn1600.Click, btn1800.Click, btn11000.Click
        Dim frmQ As New frmQuestion
        Dim vwQuestion As New DataView(dtQuestions)
 
        'hook up array
        For x = 0 To m_Categories.Rows.Count - 1
            vwQuestion.RowFilter = "CategoryID = " & m_Categories.Rows(x).Item(1)
            frmQ.Question = vwQuestion.ToTable.Rows(sender.tag).Item(2)
            frmQ.Answer1 = vwQuestion.ToTable.Rows(sender.tag).Item(3)
            frmQ.Answer2 = vwQuestion.ToTable.Rows(sender.tag).Item(4)
            frmQ.Answer3 = vwQuestion.ToTable.Rows(sender.tag).Item(5)
            frmQ.Answer4 = vwQuestion.ToTable.Rows(sender.tag).Item(6)
            frmQ.CorrectAnswer = vwQuestion.ToTable.Rows(sender.tag).Item(7)
        Next
    End Sub
 
Public Class frmQuestion
    Inherits System.Windows.Forms.Form
    'Private m_Categories As DataTable
    Private m_Question As String
    Private m_Answer1 As String
    Private m_Answer2 As String
    Private m_Answer3 As String
    Private m_Answer4 As String
    Private m_CorrectAnswer As String
 
    Public Property Question() As String
        Get
            Return m_Question
        End Get
        Set(ByVal value As String)
            m_Question = value
        End Set
    End Property
 
    Public Property Answer1() As String
        Get
            Return m_Answer1
        End Get
        Set(ByVal value As String)
            m_Answer1 = value
        End Set
    End Property
 
    Public Property Answer2() As String
        Get
            Return m_Answer2
        End Get
        Set(ByVal value As String)
            m_Answer2 = value
        End Set
    End Property
 
    Public Property Answer3() As String
        Get
            Return m_Answer3
        End Get
        Set(ByVal value As String)
            m_Answer3 = value
        End Set
    End Property
 
    Public Property Answer4() As String
        Get
            Return m_Answer4
        End Get
        Set(ByVal value As String)
            m_Answer4 = value
        End Set
    End Property
 
    Public Property CorrectAnswer() As String
        Get
            Return m_CorrectAnswer
        End Get
        Set(ByVal value As String)
            m_CorrectAnswer = value
        End Set
    End Property
    Private Sub frmQuestion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblTimer.Text = 15
        Timer1.Interval = 1000
        Timer1.Enabled = True
 
 
        Dim oBoard As New ClsBoard
        Dim dtQuestions As DataTable
        dtQuestions = oBoard.GetQuestion
 
        lblQuestion.Text = Me.Question
        lblOpt1.Text = Me.Answer1
        lblOpt2.Text = Me.Answer2
        lblOpt3.Text = Me.Answer3
        lblOpt4.Text = Me.Answer4
 
        Me.Show()
    End Sub
 
Imports System.Data.OleDb
Public Class ClsBoard
    Dim oBoard As ClsBoard
    Dim m_sCN As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Jeopardy07.accdb;User Id=admin;Password=;"
 
    Public Function GetCategories() As DataTable
 
        Dim cn As New OleDb.OleDbConnection(m_sCN) 'new connection
        Dim dt As New DataTable
        Dim sSQL As String = "Select * from tblCategory where Used = 'N'"
        Dim cmd As New OleDb.OleDbCommand(sSQL, cn)
        'get data
        Try
            cn.Open()
            dt.Load(cmd.ExecuteReader)
            GetCategories = dt
            cmd.Dispose()
            cn.Close()
            cn.Dispose()
 
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Function GetQuestion() As DataTable
 
        Dim cn As New OleDb.OleDbConnection(m_sCN) 'new connection
        Dim dt As New DataTable
        Dim sSQL As String = "Select * from tblQuestion"
        Dim cmd As New OleDb.OleDbCommand(sSQL, cn)
        'get data
        Try
            cn.Open()
            dt.Load(cmd.ExecuteReader)
            GetQuestion = dt
            cmd.Dispose()
            cn.Close()
            cn.Dispose()
 
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
End Class

Open in new window

Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

The error seems to be because the variable m_Categories is never assigned. inserting this into line 37 above:

m_Categories = dtCategories
Me.Show()
...
Avatar of 98fatboyrider
98fatboyrider

ASKER

Inserting "m_Categories = dtCategories" into line 37 produces the error  "The expression contains an invalid string constant: 's Music"  at line 46 which I believe is coming from the category "80's Music" (the first category in my database).
That seems to be a separate issue. You need to wrap a string in apostrophe ('). But you have a problem because your string contains an apostrophe itself, so you need to escape-code that. Try change line 46 to below:

vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item(1)).Replace("'", "\'") & "'"
Thanks, I see what you mean. After inserting the line you suggested, I get the error "Object reference not set to an instance of an object" at line 47.
This is another case of uninitialised variable dtQuestions, which you use in line 42.
42        Dim vwQuestion As New DataView(dtQuestions)

This variable seem to be declared on line 7, but is never initialised.
7    Dim dtQuestions As DataTable

Now, I'm not really sure where you should initialise this variable, I didn't really pay attention to your program flow...
I see. Basicly the flow is when clicking on a button on the frmBoard, it should pull the question and answer options from the database based on the CategoryID and populate the frmQuestion textboxes. The categories are filling the datatable(dtCategories) and populating the board textboxes, but when clicking on one of the question buttons, I'm receiving the error Object reference not set to an instance of an object. I'm providing the updated code.
Public Class frmBoard
    Inherits System.Windows.Forms.Form
 
    Private m_Categories As DataTable
    Private m_Question As String
 
    Dim dtQuestions As DataTable
    Dim arrCol1(4) As Button
    Dim arrCol2(4) As Button
    Dim arrCol3(4) As Button
    Dim arrCol4(4) As Button
    Dim arrCol5(4) As Button
    Dim arrCol6(4) As Button
 
    Private Sub frmBoard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim oBoard As New ClsBoard
        Dim dtCategories As New DataTable
 
        'hook up array
        arrCol1(0) = btn1200
        arrCol1(1) = btn1400
        arrCol1(2) = btn1600
        arrCol1(3) = btn1800
        arrCol1(4) = btn11000
 
        dtCategories = oBoard.GetCategories
        txtCat1.Text = dtCategories.Rows(0).Item(1)
        txtCat2.Text = dtCategories.Rows(1).Item(1)
        txtCat3.Text = dtCategories.Rows(2).Item(1)
        txtCat4.Text = dtCategories.Rows(3).Item(1)
        txtCat5.Text = dtCategories.Rows(4).Item(1)
        txtCat6.Text = dtCategories.Rows(5).Item(1)
        For x = 0 To 4
            arrCol1(x).Text = ("$" & (x + 1) * 200)
            arrCol1(x).Tag = x
        Next
        m_Categories = dtCategories
        Me.Show()
    End Sub
 
    Private Sub btnCol1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1200.Click, btn1400.Click, btn1600.Click, btn1800.Click, btn11000.Click
        Dim frmQ As New frmQuestion
        Dim vwQuestion As New DataView(dtQuestions)
 
        'hook up array
        For x = 0 To m_Categories.Rows.Count - 1
            vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item(1)).Replace("'", "\'") & "'"
            'vwQuestion.RowFilter = "CategoryID = " & m_Categories.Rows(x).Item(1)
            frmQ.Question = vwQuestion.ToTable.Rows(sender.tag).Item(2)
            frmQ.Answer1 = vwQuestion.ToTable.Rows(sender.tag).Item(3)
            frmQ.Answer2 = vwQuestion.ToTable.Rows(sender.tag).Item(4)
            frmQ.Answer3 = vwQuestion.ToTable.Rows(sender.tag).Item(5)
            frmQ.Answer4 = vwQuestion.ToTable.Rows(sender.tag).Item(6)
            frmQ.CorrectAnswer = vwQuestion.ToTable.Rows(sender.tag).Item(7)
        Next
    End Sub
 
Public Class frmQuestion
    Inherits System.Windows.Forms.Form
    Private m_Question As String
    Private m_Answer1 As String
    Private m_Answer2 As String
    Private m_Answer3 As String
    Private m_Answer4 As String
    Private m_CorrectAnswer As String
 
    Public Property Question() As String
        Get
            Return m_Question
        End Get
        Set(ByVal value As String)
            m_Question = value
        End Set
    End Property
 
    Public Property Answer1() As String
        Get
            Return m_Answer1
        End Get
        Set(ByVal value As String)
            m_Answer1 = value
        End Set
    End Property
 
    Public Property Answer2() As String
        Get
            Return m_Answer2
        End Get
        Set(ByVal value As String)
            m_Answer2 = value
        End Set
    End Property
 
    Public Property Answer3() As String
        Get
            Return m_Answer3
        End Get
        Set(ByVal value As String)
            m_Answer3 = value
        End Set
    End Property
 
    Public Property Answer4() As String
        Get
            Return m_Answer4
        End Get
        Set(ByVal value As String)
            m_Answer4 = value
        End Set
    End Property
 
    Public Property CorrectAnswer() As String
        Get
            Return m_CorrectAnswer
        End Get
        Set(ByVal value As String)
            m_CorrectAnswer = value
        End Set
    End Property
    Private Sub frmQuestion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblTimer.Text = 15
        Timer1.Interval = 1000
        Timer1.Enabled = True
 
 
        Dim oBoard As New ClsBoard
        Dim dtQuestions As DataTable
        dtQuestions = oBoard.GetQuestion
 
        lblQuestion.Text = dtQuestions.ToString
        'lblOpt1.Text = Me.Answer1
        'lblOpt2.Text = Me.Answer2
        'lblOpt3.Text = Me.Answer3
        'lblOpt4.Text = Me.Answer4
 
        Me.Show()
    End Sub
 
Imports System.Data.OleDb
Public Class ClsBoard
    Dim oBoard As ClsBoard
    Dim m_sCN As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\Jeopardy07.accdb;User Id=admin;Password=;"
 
    Public Function GetCategories() As DataTable
 
        Dim cn As New OleDb.OleDbConnection(m_sCN) 'new connection
        Dim dt As New DataTable
        Dim sSQL As String = "Select * from tblCategory where Used = 'N'"
        Dim cmd As New OleDb.OleDbCommand(sSQL, cn)
        'get data
        Try
            cn.Open()
            dt.Load(cmd.ExecuteReader)
            GetCategories = dt
            cmd.Dispose()
            cn.Close()
            cn.Dispose()
 
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Function GetQuestion() As DataTable
 
        Dim cn As New OleDb.OleDbConnection(m_sCN) 'new connection
        Dim dt As New DataTable
        Dim sSQL As String = "Select * from tblQuestion"
        Dim cmd As New OleDb.OleDbCommand(sSQL, cn)
        'get data
        Try
            cn.Open()
            dt.Load(cmd.ExecuteReader)
            GetQuestion = dt
            cmd.Dispose()
            cn.Close()
            cn.Dispose()
 
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
End Class

Open in new window

Then you probably need to call ClsBoard.GetQuestion or something on line 43

Dim board As New ClsBoard()
Dim vwQuestion As New DataView(board.GetQuestion())
That produces the error "Cannot perform '=' operation on System.Int32 and System.String." at line 46. I can't help but to think I'm missing something in the form load.
That's strange, I don't see any comparison operator = at line 46. Are you sure it's on this line?
        For x = 0 To m_Categories.Rows.Count - 1
Sorry, I'm recieving the error on line 47
vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item(1)).Replace("'", "\'") & "'"
ASKER CERTIFIED SOLUTION
Avatar of philipjonathan
philipjonathan
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That worked! The questions and answers are populating the datatable, but now how do I get the question and answers to populate the labels on frmQuestion?
You've been a great help. Thank you.