Solved

Stuck creating Jeopardy style game

Posted on 2008-10-07
13
880 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:98fatboyrider
  • 7
  • 6
13 Comments
 
LVL 18

Expert Comment

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

m_Categories = dtCategories
Me.Show()
...
0
 

Author Comment

by:98fatboyrider
ID: 22665396
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).
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22665428
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("'", "\'") & "'"
0
 

Author Comment

by:98fatboyrider
ID: 22665520
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.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22665772
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...
0
 

Author Comment

by:98fatboyrider
ID: 22665869
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

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 18

Expert Comment

by:philipjonathan
ID: 22665988
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())
0
 

Author Comment

by:98fatboyrider
ID: 22666121
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.
0
 
LVL 18

Expert Comment

by:philipjonathan
ID: 22666141
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
0
 

Author Comment

by:98fatboyrider
ID: 22667981
Sorry, I'm recieving the error on line 47
vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item(1)).Replace("'", "\'") & "'"
0
 
LVL 18

Accepted Solution

by:
philipjonathan earned 500 total points
ID: 22674938
Wait a minute, what is the data type of your CategoryID?
Is your category table looks like this:
CategoryID    CategoryName
------------------------------------
1                    80's Music
2                    Rock 'n Roll
3                    ...

If that's the case, try:
vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item("CategoryID")).Replace("'", "\'") & "'"

or:
vwQuestion.RowFilter = "CategoryID = '" & CStr(m_Categories.Rows(x).Item(0)).Replace("'", "\'") & "'"
0
 

Author Comment

by:98fatboyrider
ID: 22675329
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?
0
 

Author Closing Comment

by:98fatboyrider
ID: 31504069
You've been a great help. Thank you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now