Hello:(

I have an assignment to build the booking and payment part of a Thearte Booking System. I have four classes linked to an MDF database
- well they should be - the two that we have been given will will be and the two that I have done might be!

The first thing that I have to do is test the classes on a test form.

As an example I have put the SheduledEvents class at the bottom of the post (one that I have been given so it should be correct).

So, as I understand it - I have to return a lsit of scheduled events to the test form, say in a list box, from a ScheduledEventSortedList.
I have spent two days trying to return the data row for scheduledEventID on my test form and failed. There is an example of one attempt
of many below, and like all the others, it doesn't work.

namespace TheatreTicketBooking
{
    public partial class ScheduledEventsTestForm : Form
    {
        int ScheduledEventID = 0;
        ScheduledEventsTestForm[] TestEvent;

        public ScheduledEventsTestForm(int SEID)//SEID for ScheduledEventID
        {
            InitializeComponent();
            
            lstEventData.Text = " Scheduled Event ID:  " + ScheduledEventID;
            return drEventList[0];             
    }
}

Could someone please point me in the right direction

All the best John:)

Here is the class that I am testing

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//added these
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace TheatreTicketBooking
{
    class ScheduledEvent
    {
        private int m_ScEventID;
        private string m_EventName;
        private DateTime m_DateOfEvent;
        private string m_TimeOfEvent;
        private decimal m_AdultPrice;
        private decimal m_ConcessionPrice;
        private decimal m_FriendPrice;
        private string m_Confirmed;
        private string m_Artist;
        private string m_Location;

        // Declare an ScheduledEventID property of type int:
        public int ScheduledEventID
        {
            get
            {
                return m_ScEventID;
            }

            set
            {
                m_ScEventID = value;
            }
        }

        // Declare an EventName property of type string:
        public string EventName
        {
            get
            {
                return m_EventName;
            }
            set
            {
                m_EventName = value;
            }
        }

        // Declare a DateOfEvent property of type DateTime:
        public DateTime DateOfEvent
        {
            get
            {
                return m_DateOfEvent;
            }
            set
            {
                m_DateOfEvent = value;
            }
        }

        // Declare a TimeOfEvent property of type string:
        public string TimeOfEvent
        {
            get
            {
                return m_TimeOfEvent;
            }
            set
            {
                m_TimeOfEvent = value;
            }
        }

        // Declare an AdultPrice property of type decimal:
        public decimal AdultPrice
        {
            get
            {
                return m_AdultPrice;
            }
            set
            {
                m_AdultPrice = value;
            }
        }
        
        // Declare a ConcessionPrice property of type decimal:
        public decimal ConcessionPrice
        {
            get
            {
                return m_ConcessionPrice;
            }
            set
            {
                m_ConcessionPrice = value;
            }
        }

        // Declare a FriendPrice property of type decimal:
        public decimal FriendPrice
        {
            get
            {
                return m_FriendPrice;
            }
            set
            {
                m_FriendPrice = value;
            }
        }


        // Declare a Confirmed property of type string:
        public string Confirmed
        {
            get
            {
                return m_Confirmed;
            }
            set
            {
                m_Confirmed = value;
            }
        }

        // Declare an Artist property of type string:
        public string Artist
        {
            get
            {
                return m_Artist;
            }
            set
            {
                m_Artist = value;
            }
        }

        // Declare a Location property of type string:
        public string Location
        {
            get
            {
                return m_Location;
            }
            set
            {
                m_Location = value;
            }
        }

        //For a given ScheduledEventID, retrieve a ScheduledEvent object
        public void GetEventByID(int ScheduledEventID)//being passed this ID  (intCutomer No oncustomers
        {
            string TBConnectionString = ConfigurationManager.ConnectionStrings["TicketBookingConnectionString"].ConnectionString;

            SqlConnection cnTB = new SqlConnection(TBConnectionString);
            cnTB.Open();
            SqlCommand cmEvent = new SqlCommand();
            cmEvent.Connection = cnTB;
            cmEvent.CommandType = CommandType.Text;
            cmEvent.CommandText = "Select * from ScheduledEvents where ScheduledEventID = " + ScheduledEventID;//star - read it all
            SqlDataReader drEvent = cmEvent.ExecuteReader();
            drEvent.Read();// go and read the data

            //the variables
            m_ScEventID = (int)drEvent[0];
            m_EventName = drEvent[1].ToString();//
            m_DateOfEvent = (DateTime)drEvent[2];
            m_TimeOfEvent = drEvent[3].ToString();
            m_AdultPrice = (decimal)drEvent[4];
            m_ConcessionPrice = (decimal)drEvent[5];
            m_FriendPrice = (decimal)drEvent[6];
            m_Confirmed = drEvent[7].ToString();
            m_Artist = drEvent[8].ToString();
            m_Location = drEvent[9].ToString();
            drEvent.Close();
            cnTB.Close();
        }


        //Return a SortedList of ScheduledEvent objects for a given date
        public SortedList ListEventsByDateOfEvent(DateTime EventDate)
        {
            string TBConnectionString = ConfigurationManager.ConnectionStrings["TicketBookingConnectionString"].ConnectionString;

            string EventDateString = EventDate.ToString("MM/dd/yyyy");
            SqlConnection cnTB = new SqlConnection(TBConnectionString);
            cnTB.Open();
            SqlCommand cmEventList = new SqlCommand();
            cmEventList.Connection = cnTB;
            cmEventList.CommandType = CommandType.Text;
            cmEventList.CommandText = "Select ScheduledEventID, EventName, ArtistName, Location, Confirmed from ScheduledEvents where DateOfEvent = '" + EventDateString + "'";
            SqlDataReader drEventList = cmEventList.ExecuteReader();

            SortedList EventList = new SortedList();//creating a new sorted list

            while (drEventList.Read())// while it reads the data rows one at a time
            {
                if (drEventList[4].ToString() == "Y")
                {
                    EventList.Add(drEventList[0], drEventList[1].ToString() + " - " + drEventList[2].ToString() + " - " + drEventList[3].ToString());// list the fields
                }
            }
            drEventList.Close();
            cnTB.Close();
            return EventList;
        }
    }
}

Recommended Answers

All 8 Replies

What is wrong with this code? You only posted two methods, which can tell me nothing.
You have to be more precise about what do you want to achive.
Can you please do a bit better explanation, or add some more code? It would really help out.

Your form class should look more like this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ScheduledEvent currentEvent;
        public Form1()
        {
            InitializeComponent();
            //currentEvent is going to be our scheduledEvent object which will store whatever scheduledEvent the user is currently looking at.
            //When you first instantiate currentEvent, there is nothing in it. You have to use GetEventByID to fill it.
            currentEvent = new ScheduledEvent();
        }

        private void getaScheduledEventButton_Click(object sender, EventArgs e)
        {
            //The user has pressed a button labelled "Find event". The event id typed in by the user into textBox1 is cast into an int and sent to the GetEventByID method.
            //IF the number corresponds to a real event id, then the fields of currentEvent will be updated.
            currentEvent.GetEventByID(int.Parse(textBox1.Text));
            //Assuming the fields of currentEvent have updated, output the fields of currentEvent to whatever other textboxes you have, so the user can see the information
        }

        private void getAllEventsButton_Click(object sender, EventArgs e)
        {
            //If the user presses a button named "Show all events on Date", use the ListEventsByDateOfEvent method. This should return all ScheduledEvent objects contained in
            //the database which have the same date as DateTime.Now.
            SortedList allEvents = currentEvent.ListEventsByDateOfEvent(DateTime.Now);
            
            //Once allEvents gets all of the events on a certain date, you need to show them to the user. Something like below will do that for you. You may have to put it in a loop
            //and change the '0' to an index which increments.
            listBox1.Items.Add(allEvents.GetByIndex(0));
        }
    }
}
commented: Really great help - thanks +1

What is wrong with this code? You only posted two methods, which can tell me nothing.
You have to be more precise about what do you want to achive.
Can you please do a bit better explanation, or add some more code? It would really help out.

Hi Mitja,

Thanks for the help. Sorry, I'm having trouble explaining it because I'm having trouble understanding it:icon_smile:

I am trying to do this task: • Task 1 requires you to complete the coding of the four classes required for the Ticket Booking program. For each of these four classes you must also build a windows test client which you use to verify the functionality of the classes

As far as I know there is nothing wrong with the class ScheduledEvent code. What I need to do is access that code from another form to test it. This is where my problems begin as I am not sure how to test it other than show that I can get the information held in the database to show in a list box on the test form. This is where I am having lots of trouble.

My understanding of what is happening is - the sql statement in the class deals with the data from the database, so to test it is correct I have created the ScheduledEventsTestForm class (on the back of the test form) and tried to return the integer SheduledEventID. From there I was hoping that I could work out how to return the other data rows or an event list.

These are the fields in the database with their data types that I am trying to return.

Column Name Data Type Allow Nulls

ScheduledEventID int Unchecked
EventName varchar(40) Unchecked
DateOfEvent datetime Unchecked
TimeOfEvent char(5) Checked
AdultTicketPrice money Checked
ConcessionTicketPrice money Checked
FriendsTicketPrice money Checked
Confirmed char(1) Checked
ArtistName varchar(40) Checked
Location varchar(25) Checked

Sorry - I can't explain it more than that!
Thanks again ... John.

Create a new DataTable and fill it up with the values from all those fields (in dataBase), using a correct query:

private void ShowData()
{
    int scheduledId = 1; //some example
    DataTable table = GetData(scheduledId);
    if(table.Rows.Count > 0)
    {
         //use data from dataTable to populate some contols, or dataGridView.
         //1. if controls, like textboxes
         foreach(DataRow dr in table.Rows)
         {
             textBox1.Text = dr[0].Tostring(); //index 0 is 1st column!
             //and so on...
         }

         //2. is you want to populate dataGridView, you can set table as a data source to dgv:
         //no need to create columns and stuff for dgv (it will create all automatically)
         dataGridView1.DataSource = new BindingSource(table, null);
    }
}
private DataTable GetData(int ScheduledEventID)
{
     SqlConnection sqlConn = new SqlConnection("connString");
     string query = @"Select * from ScheduledEvents where ScheduledEventID = @id";
     SqlCommand cmd = new SqlCommand(query, sqlConn);
     cmd.Parameters.Add("@id", SqlDbType.Int).Value = ScheduledEventID;
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable table = new DataTable("MyTable");
     da.Fill(table);
     return table;
}

I hope this helps explaining how you get data and use them later in populating controls.

commented: Always really helpful - thank you +1

Your form class should look more like this:]

Wow kimbokasteniv ... thanks for that :)

... even with my brain frazzled from two days of getting nowhere I am sure I can do what I want with what you have given me there. Then I should be able to apply it to the other classes. (It's nice to recognize some of the things that I tried too - only done effectively).

Thanks again ... John.

Create a new DataTable and fill it up with the values from all those fields (in dataBase), using a correct query:

private void ShowData()
{
    int scheduledId = 1; //some example
    DataTable table = GetData(scheduledId);
    if(table.Rows.Count > 0)
    {
         //use data from dataTable to populate some contols, or dataGridView.
         //1. if controls, like textboxes
         foreach(DataRow dr in table.Rows)
         {
             textBox1.Text = dr[0].Tostring(); //index 0 is 1st column!
             //and so on...
         }

         //2. is you want to populate dataGridView, you can set table as a data source to dgv:
         //no need to create columns and stuff for dgv (it will create all automatically)
         dataGridView1.DataSource = new BindingSource(table, null);
    }
}
private DataTable GetData(int ScheduledEventID)
{
     SqlConnection sqlConn = new SqlConnection("connString");
     string query = @"Select * from ScheduledEvents where ScheduledEventID = @id";
     SqlCommand cmd = new SqlCommand(query, sqlConn);
     cmd.Parameters.Add("@id", SqlDbType.Int).Value = ScheduledEventID;
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable table = new DataTable("MyTable");
     da.Fill(table);
     return table;
}

I hope this helps explaining how you get data and use them later in populating controls.

Hi again Mitja ...

thanks - aha ... that's brilliant, and should keep me quiet and out of trouble for another two days at least. I can see lots of places that I should be able to apply this :).

Thanks again ... John

Iam glad you are satisfied. There are still areas to improve the code, but one step at the time.
You should also consider doing this code in 3 tier architecture. It means you build your project, expecially when you work with dataBase, in seperated classes. Access to the database is 1st class (DAL - Data Access Layer), if there is any code to do any calculations and other stuff is 2nd class (BLL -Business Logic Layer), and the last one (3rd) is the GUI (Grapgical User Interface).

But as said, one step at the time.

Iam glad you are satisfied. There are still areas to improve the code, but one step at the time.
You should also consider doing this code in 3 tier architecture. It means you build your project, expecially when you work with dataBase, in seperated classes. Access to the database is 1st class (DAL - Data Access Layer), if there is any code to do any calculations and other stuff is 2nd class (BLL -Business Logic Layer), and the last one (3rd) is the GUI (Grapgical User Interface).

But as said, one step at the time.

Hahaha ... I'm satisfied because I undestand some of your solutions, and am grateful for them as I seem to get quite stuck.
As for 'one step at a time', I find I'm not so fast at learning coding and I'm definitely running before I can walk to get my assignments in on time. That's why I keep falling over (like my programs)... I'll probably be back in a few days asking to have things explained again!

:)John.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.