Solved

Stuck creating Jeopardy style game

Posted on 2008-10-07
13
898 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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
 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 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