Solved

Stuck creating Jeopardy style game

Posted on 2008-10-07
13
883 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 42
vb.net 2 37
C# HTTP GET method sample code 3 41
Capture logon name 13 46
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

895 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

13 Experts available now in Live!

Get 1:1 Help Now